-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.java
More file actions
34 lines (30 loc) · 790 Bytes
/
Search.java
File metadata and controls
34 lines (30 loc) · 790 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
31
32
33
34
/**
* 33. 搜索旋转排序数组
*/
public class Search {
public int search(int[] nums, int target) {
int left=0, right=nums.length-1;
int res = -1;
int mid;
while (left<=right){
mid = (left+right)/2;
if(nums[mid]==target){
return mid;
}
if(nums[0]<= nums[mid]){
if(nums[0]<=target && target<nums[mid]){
right = mid-1;
}else{
left = mid +1;
}
}else{
if(nums[mid]<target && target<=nums[nums.length-1]){
left = mid +1;
}else{
right = mid-1;
}
}
}
return res;
}
}