-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsearchInRotatedSortedArray.java
More file actions
45 lines (45 loc) · 1.35 KB
/
searchInRotatedSortedArray.java
File metadata and controls
45 lines (45 loc) · 1.35 KB
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
public class searchInRotatedSortedArray {
//Approach: 2 pass binary search
/* first find the minimum num, where the rotate begins
then I will find where will be the target. min's left or min's right.
In the subarray, it will be an increasing array. So it's time to do regular binary search.
* */
public int search(int[] nums, int target){
if(nums == null || nums.length == 0){
return -1;
}
int start = 0, end = nums.length - 1;
int mid;
while(start + 1 < end){
mid = start + (end - start)/2;
if(nums[mid] < nums[end]){
end = mid;
}else{
start = mid;
}
}
int midpoint = (nums[start] < nums[end]) ? start : end;
if(target > nums[nums.length - 1]){
start = 0;
end = -1 + midpoint == 0? 1 : midpoint;
}else{
start = midpoint;
end = nums.length - 1;
}
while(start + 1 < end){
mid = start + (end - start)/2;
if(nums[mid] < target){
start = mid;
}else{
end = mid;
}
}
if(nums[start] == target){
return start;
}else if(nums[end] == target){
return end;
}else{
return -1;
}
}
}