-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchRotateArray2.java
More file actions
55 lines (49 loc) · 1.58 KB
/
SearchRotateArray2.java
File metadata and controls
55 lines (49 loc) · 1.58 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
46
47
48
49
50
51
52
53
54
55
package LeetCodeOJ;
import java.util.Queue;
import java.util.Stack;
/* 81.Search in Rotated Sorted Array II
* Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
*/
public class SearchRotateArray2 {
/*
* ��ͬ�ķ�����leetcode��վ�ύ�Ľ��
*/
public boolean search(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
int mid = 0;
while (low <= high) {
// ����ļ����д����Դ������ط���˼�롣
mid = (low + high) / 2;
if (target == nums[mid]) {
return true;
}
if (nums[low] < nums[mid]) {
// �������
if (target < nums[mid] && target >= nums[low]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else if (nums[mid] < nums[high]) {
// ��������ұ�����
if (target > nums[mid] && target <= nums[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
// low == mid 可能是出现多个元素
else {
low++;
}
}
return false;
}
public static void main(String[] args) {
int[] n = { 4, 5, 6, 7, 7, 8, 0, 1, 1, 2, 3 };
// int [] n ={1,0,1,1,1,1,1};
System.out.println(new SearchRotateArray2().search(n, 1));
}
}