-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInRotatedSortedArrayII.java
More file actions
37 lines (36 loc) · 1.02 KB
/
Copy pathSearchInRotatedSortedArrayII.java
File metadata and controls
37 lines (36 loc) · 1.02 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
/* Example 1 : Input : nums = [2,5,6,0,0,1,2], target = 0
* Output : true
*
* Example 2 : Input : nums = [2,5,6,0,0,1,2], target = 3
* Output : false
**/
class SearchInRotatedSortedArrayII {
public boolean search(int[] nums, int target) {
int left = 0;
int right = nums.length -1;
while(left <= right){
int mid = left + (right - left) / 2;
if(nums[mid] == target){
return true;
}
if(nums[mid] == nums[left]){
left++;
continue;
}
if(nums[left] <= nums[mid]){
if(nums[left] <= target && nums[mid] > target){
right = mid -1;
}else{
left = mid + 1;
}
} else{
if(nums[mid] < target && nums[right] >= target){
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return false;
}
}