-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchrotatearray.cpp
53 lines (46 loc) · 1.24 KB
/
searchrotatearray.cpp
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
47
48
49
50
51
52
53
class Solution {
public:
int tar;
int bsearch(int A[],int begin,int end){
if(begin > end)
return -1;
int mid = (begin+end)/2;
if(A[mid] > tar){
if(A[mid] < A[begin])
return bsearch(A,begin,mid-1);
else{
if(tar >= A[begin]){
return bsearch(A,begin,mid-1);
}
else{
return bsearch(A,mid+1,end);
}
}
}
else if(A[mid] < tar){
if(A[mid] > A[end]){
return bsearch(A,mid+1,end);
}
else{
if(tar <= A[end]){
return bsearch(A,mid+1,end);
}
else{
return bsearch(A,begin,mid-1);
}
}
}
else
return mid;
}
int search(int A[], int n, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int result = -1;
if(n == 0)
return result;
tar = target;
result = bsearch(A,0,n-1);
return result;
}
};