-
Notifications
You must be signed in to change notification settings - Fork 0
/
81.search-in-rotated-sorted-array-ii.java
43 lines (39 loc) · 1.26 KB
/
81.search-in-rotated-sorted-array-ii.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
class Solution {
public boolean search(int[] nums, int target) {
if(nums.length == 0)
return false;
return bsearch(nums, target, 0, nums.length-1);
}
public boolean bsearch(int[] nums, int target, int l,int e){
if(l<0 || e>= nums.length)
return false;
while(l<=e){
int mid = (l+e)/2;
if(nums[mid] == target )
return true;
else if(nums[l] < nums[mid]){
if(nums[mid] > target && nums[l] <= target)
e = mid-1;
else
l = mid+1;
}else if(nums[mid] < nums[e]){
if(nums[mid] < target && nums[e] >= target)
l = mid+1;
else
e = mid-1;
}
// else if()
// return bsearch(nums, target, l, mid-1) || bsearch(nums, target, mid+1, r);
else if(nums[mid] == nums[l] && nums[mid] == nums[e])
{
l++;
e--;
}
else if(nums[mid] == nums[l])
l = mid+1;
else if(nums[mid] == nums[e])
e= mid-1;
}
return false;
}
}