forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0033-search-in-rotated-sorted-array.java
47 lines (44 loc) · 1.41 KB
/
0033-search-in-rotated-sorted-array.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
38
39
40
41
42
43
44
45
46
47
class Solution {
public int search(int[] nums, int target) {
int minIndex = minIndex(nums);
int leftSearch = binarySearch(nums, 0, minIndex - 1, target);
int rightSearch = binarySearch(nums, minIndex, nums.length - 1, target);
if (leftSearch != -1) {
return leftSearch;
} else if (rightSearch != -1) {
return rightSearch;
} else {
return -1;
}
}
public int minIndex(int[] nums) {
int start = 0;
int end = nums.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
int prev = (mid - 1 + nums.length) % nums.length;
int next = (mid + 1) % nums.length;
if (nums[mid] <= nums[prev] && nums[mid] <= nums[next]) {
return mid;
} else if (nums[mid] <= nums[end]) {
end = mid - 1;
} else if (nums[mid] >= nums[start]) {
start = mid + 1;
}
}
return -1;
}
public int binarySearch(int[] nums, int start, int end, int target) {
while (start <= end) {
int mid = start + (end - start) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -1;
}
}