数组中部分有序数据查找某个数

来源:互联网 发布:皇茶哪个是正宗的知乎 编辑:程序博客网 时间:2024/06/03 13:31

今天一哥们去美团面试碰到个面试题目,跟我吐槽没做好,题目大概思想是一个有序的数组,把前面有序的一部分放到数组后面,是整个数组变成部分有序,也就是先递增,中断,然后再递增:

具体如下:
 本来是 2  5  9  11  12  15  16 ,现在把最后的几个放在前面,
就出现 12  15  16  2  5  9  11  这样的了
判断某个数字在不在数组中 ,不接受复杂度高于O(N)的查找,



晚上用java代码实现了下,基本思想是先利用二分查找找到最大数的位置,

然后再用二分查找查找找出target的位置,找到返回目标值,没找到返回-1

算法复杂度为O(logn)



package com.iclick.aerospike;public class App {public static void main(String[] args) {int[] arr = { 11, 12, 15, 16, 18, 19, 33, 40, 1, 2, 5, 6, 8, 10 };System.out.println(search(arr, 33));}public static int search(int[] arr, int target) {int result = -1;int maxPosition = searchMaxPosition(arr);if (target > arr[maxPosition] || target < arr[maxPosition + 1]) {result = -1;} else if (target <= arr[maxPosition] && target >= arr[0]) {result = binearSearch(arr, 0, maxPosition, target);} else if (target >= arr[maxPosition + 1]&& target <= arr[arr.length - 1]) {result = binearSearch(arr, maxPosition + 1, arr.length - 1, target);}return result;}public static int binearSearch(int arr[], int start, int end, int target) {int middle = 0;while (start <= end) {middle = (start + end) / 2;if (arr[middle] == target) {return target;} else if (arr[middle] > target) {end = middle - 1;} else if (arr[middle] < target) {start = middle + 1;}}return -1;}public static int searchMaxPosition(int[] arr) {int start = 0;int end = arr.length - 1;int middle = 0;while (true) {middle = (start + end) / 2;if (arr[middle] > arr[start]) {start = middle;} else if (arr[middle] < arr[end]) {end = middle;} else {return start;}}}/* * public static int searchMaxPosition(int[] arr) { int start = 0; int end = * arr.length - 1; int middle = 0; while (start < end) { middle = (start + * end) / 2; *  * if (arr[middle] > arr[middle + 1] && arr[middle] > arr[middle - 1]) { * //这样的数一定存在,就是数组中最大的数 break; } else if (arr[middle] > arr[middle - 1] && * arr[middle] < arr[arr.length - 1]) { *  * end = middle; *  * } else if (arr[middle] > arr[middle - 1] && arr[middle] > arr[arr.length * - 1]) { start = middle; *  * } else { end = middle; } } return middle; *  * } */}


0 0
原创粉丝点击