-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchInRotatedSortedArrayII.java
48 lines (42 loc) · 1.07 KB
/
SearchInRotatedSortedArrayII.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
48
/**
* @(#)SearchInRotatedSortedArrayII.java, Jul 06, 2020.
*
* Copyright 2020 Lillard. All rights reserved. Use is subject to license terms.
*/
/**
* @author Lillard
*/
public class SearchInRotatedSortedArrayII {
class Solution {
public boolean search(int[] nums, int target) {
if (nums == null || nums.length <= 0) {
return false;
}
return search(nums, target, 0, nums.length - 1) >= 0;
}
private int search(int[] nums, int target, int left, int right) {
while (left <= right) {
int mid = (left + right) / 2;
if (nums[left] == target) {
return left;
}
if (nums[left] < nums[mid]) {
if (target > nums[mid] || target < nums[left]) {
left = mid + 1;
} else {
right = mid;
}
} else if (nums[left] > nums[mid]) {
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
} else {
right = mid;
}
} else {
left++;
}
}
return -1;
}
}
}