-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode_33.java
37 lines (35 loc) · 958 Bytes
/
leetcode_33.java
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
package leetcode;
public class leetcode_33 {
/*
搜索旋转排序数组
*/
public int search(int[] nums, int target) {
int len = nums.length;
if (len == 0) {
return -1;
}
if (len == 1) {
return nums[0] == target ? 0 : -1;
}
int l = 0, r = len - 1;
while (l < r) {
int mid = (l + r) / 2;
if (mid == target)
return mid;
if (nums[0] <= nums[mid]) {
if (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[len - 1]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
}