-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT117.java
More file actions
30 lines (26 loc) · 757 Bytes
/
T117.java
File metadata and controls
30 lines (26 loc) · 757 Bytes
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
/**
* @Author:Aliyang
* @Data: Created in 上午11:35 18-6-16
* search-in-rotated-sorted-array:我的解法
* 思路:二分
**/
public class T117 {
public int search(int[] A, int target) {
int start=0,end=A.length-1;
while (start<=end){
int mid=(start+end)/2;
if (A[mid]==target)
return mid;
if (A[start]<=A[mid]){//mid在左边旋转部分
if (target>=A[start]&&target<A[mid])
end=mid-1;
else start=mid+1;
}else {//mid在右边旋转部分
if (A[mid]<target&&target<=A[end])
start=mid+1;
else end=mid-1;
}
}
return -1;
}
}