Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array.Your algorithm’s runtime complexity must be in the order of O(log n).
publicintsearch(int[] nums, int target){ if (nums == null || nums.length == 0) { return -1; } int low = 0, high = nums.length - 1; int mid; while (low <= high) { mid = (low + high) / 2; if (nums[mid] == target) { return mid; } if (nums[mid] >= nums[low]) { if (nums[mid] > target && target >= nums[low]) { high = mid - 1;//Target is in the left which is order. } else { low = mid + 1;//Target is in the right which may be disorder. } } else { if (nums[mid] < target && target <= nums[high]) { low = mid + 1;//Target is in the right which is order. } else { high = mid - 1;//Target is in the left which may be disorder. } } } return -1; }