/** * 先简单处理下,找到tmp临界点,再确定左右范围 * * @param nums * @param target * @return */ public static int search_1(int[] nums, int target) { // write code here int right = nums.length - 1; int left = 0; int tmp = 0; int j = 0; for (int i = 0; i < nums.length; i++) { if (i + 1 < nums.length) { if (nums[i + 1] < nums[i]) { tmp = nums[i]; j = i; break; } } } if (nums[right] > target && target > tmp) { left = j; } else if (nums[0] < target && target < tmp) { right = j; } while (left <= right) { int mid = (left + right) / 2; if (nums[mid] == target) { return mid; } else if (nums[mid] > target) { right = mid - 1; } else if (nums[mid] < target) { left = mid + 1; } } return -1; }