-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path81. Search in Rotated Sorted Array II
52 lines (45 loc) · 1.15 KB
/
81. Search in Rotated Sorted Array II
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
48
49
50
51
52
class Solution {
public boolean search(int[] nums, int target)
{
int n=nums.length;
int l=0;
int h=n-1;
while(l<=h)
{
int mid=(l+h)/2;
if(nums[mid]==target)
{
return true;
}
if (nums[l] == nums[mid] && nums[mid] == nums[h]) // [1, 3, 1, 1, 1]
{
// If we have duplicates, we shrink the boundaries
l++;
h--;
}
else if (nums[l] <= nums[mid])
{
if (target >= nums[l] && target < nums[mid])
{
h = mid - 1;
}
else
{
l = mid + 1;
}
}
else
{
if (target > nums[mid] && target <= nums[h])
{
l = mid + 1;
}
else
{
h = mid - 1;
}
}
}
return false;
}
}