forked from gh877916059/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path062. 搜索旋转排序数组.cpp
57 lines (57 loc) · 1.14 KB
/
062. 搜索旋转排序数组.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
54
55
56
57
public class Solution
{
public int search(int[] A, int target)
{
if(A.length==0)
return -1;
int min_index=0;
int x=0;
int y=A.length;
int m;
if(A[0]>A[A.length-1])
{
while(x<y)
{
m=x+(y-x)/2;
if(A[m]>A[x])
{
x=m+1;
if(A[x]<A[y-1])
break;
}
else
{
y=m;
if(A[x]<A[y-1])
{
x=y;
break;
}
}
}
min_index=x;
}
if(target<=A[A.length-1])
{
x=min_index;
y=A.length;
}
else
{
x=0;
y=min_index;
}
while(x<y)
{
m=x+(y-x)/2;
if(A[m]>=target)
y=m;
else
x=m+1;
}
if(A[x]==target)
return x;
else
return -1;
}
}