1 topics
Suppose a sorted array is rotated on 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 is given a target value to search. If found in the array is return its index, otherwise return-1.
Assume no duplicate exists in the array.
Hide TagsArray Binary Search2 thought for a while, considering the target and the size of the medium and two sides to determine the next two-point search for the low and high. The result is too many branches, some problems are not considered, give up. Look at other people's ideas, Https://leetcode.com/discuss/19959/solution-logn-binary-search-solution-with-explanation-java. There must be one side of the mid-order, according to this idea, it is good to do. 3 Code
//The left or right side of mid is always lined up in a good order. //if in the order of the inside, then the simple search, otherwise it is not in the orderly side Public intSearchint[] A,inttarget) { intLow = 0; intHigh = A.length-1; while(Low <High ) { intMid = (low + high + 1)/2; if(A[mid] = = target)returnmid; if(A[mid] >A[low]) { if(Target < A[low] | | target >A[mid]) { Low= Mid + 1; } Else{ High= Mid-1; } } Else { if(Target < A[mid] | | target >A[high]) { High= Mid-1; } Else{ Low= Mid+1; } } } returnLow < A.length && A[low] = = target? Low:-1; }
[Leetcode 33] Search in rotated Sorted Array