-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path62.h
46 lines (45 loc) · 1.46 KB
/
62.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
/*
* @param A: an integer rotated sorted array
* @param target: an integer to be searched
* @return: an integer
*/
int search(vector<int> &A, int target) {
// write your code here
if(A.size() == 0) return -1;
// int rotateBase = 0;
// for(;rotateBase < A.size()-1 && A[rotateBase] < A[rotateBase+1]; rotateBase++);
// int i1 = binarySearch(A, 0, rotateBase+1, target);
// int i2 = binarySearch(A, rotateBase, A.size(), target);
// if(i1 != -1) return i1;
// else if(i2 != -1) return i2;
// else return -1;
int l = 0, r = A.size();
while(l < r){
int mid = l + (r-l)/2;
//不断的搜索有序区间
if(A[mid] == target) return mid;
else if(A[mid] < A[r-1]){ //右边为有序区间
if(target > A[mid] && target <= A[r-1]) l = mid+1;
else r = mid;
}
else{ //A[mid] > A[l]左边为有序区间
if(target >= A[l] && target < A[mid]) r = mid;
else l = mid+1;
}
}
return -1;
}
// don't use
// A[l,r)
int binarySearch(vector<int> &A, int l, int r, int target){
while(l < r){
int mid = l + (r-l)/2;
if(A[mid] == target) return mid;
else if(A[mid] < target) l = mid+1;
else r = mid;
}
return -1;
}
};