题目:
Suppose a sorted array 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.
题解:
这道题是一道常见的二分查找法的变体题。
要解决这道题,需要明确rotated sorted array的特性,那么就是至少有一侧是排好序的(无论pivot在哪,自己画看看)。接下来就只需要按照这个特性继续写下去就好。以下就以伪代码方法来说明:
- 如果target比A[mid]值要小
-
如果A[mid]右边有序(A[mid]<A[high])
- 那么target肯定不在右边(target比右边的都得小),在左边找
- 如果A[mid]左边有序
- 那么比较target和A[low],如果target比A[low]还要小,证明target不在这一区,去右边找;反之,左边找。
- 如果target比A[mid]值要大
-
如果A[mid]左边有序(A[mid]>A[low])
-
那么target肯定不在左边(target比左边的都得大),在右边找
- 如果A[mid]右边有序
- 那么比较target和A[high],如果target比A[high]还要大,证明target不在这一区,去左边找;反之,右边找。
以上实现代码如下所示:
1 public int search(int [] A,int target){
2 if(A==null||A.length==0)
3 return -1;
4
5 int low = 0;
6 int high = A.length-1;
7
8 while(low <= high){
9 int mid = (low + high)/2;
10 if(target < A[mid]){
11 if(A[mid]<A[high])//right side is sorted
12 high = mid - 1;//target must in left side
13 else
14 if(target<A[low])//target<A[mid]&&target<A[low]==>means,target cannot be in [low,mid] since this side is sorted
15 low = mid + 1;
16 else
17 high = mid - 1;
18 }else if(target > A[mid]){
19 if(A[low]<A[mid])//left side is sorted
20 low = mid + 1;//target must in right side
21 else
22 if(target>A[high])//right side is sorted. If target>A[high] means target is not in this side
23 high = mid - 1;
24 else
25 low = mid + 1;
26 }else
27 return mid;
28 }
29
30 return -1;
31 }
2 if(A==null||A.length==0)
3 return -1;
4
5 int low = 0;
6 int high = A.length-1;
7
8 while(low <= high){
9 int mid = (low + high)/2;
10 if(target < A[mid]){
11 if(A[mid]<A[high])//right side is sorted
12 high = mid - 1;//target must in left side
13 else
14 if(target<A[low])//target<A[mid]&&target<A[low]==>means,target cannot be in [low,mid] since this side is sorted
15 low = mid + 1;
16 else
17 high = mid - 1;
18 }else if(target > A[mid]){
19 if(A[low]<A[mid])//left side is sorted
20 low = mid + 1;//target must in right side
21 else
22 if(target>A[high])//right side is sorted. If target>A[high] means target is not in this side
23 high = mid - 1;
24 else
25 low = mid + 1;
26 }else
27 return mid;
28 }
29
30 return -1;
31 }
Reference:http://www.cnblogs.com/ider/archive/2012/04/01/binary_search.html