-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_in_rotated_sorted_array_II.cc
62 lines (54 loc) · 1.67 KB
/
search_in_rotated_sorted_array_II.cc
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
56
57
58
59
60
61
62
// method 1:
class Solution {
public:
bool search(int A[], int n, int target) {
int low, high, mid;
int i;
for (i = 0; i < n - 1; ++i) {
if (A[i] > A[i + 1]) break;
}
low = i + 1;
high = i + n;
while (low <= high) {
mid = (high - low) / 2 + low;
if (A[mid % n] == target) return true;
if (A[mid % n] < target) low = mid + 1;
else high = mid - 1;
}
return false;
}
};
// method 2:
class Solution {
public:
bool search(int A[], int n, int target) {
if (0 == n) return -1;
int h, l, mid;
int base = A[0];
if (base == target) return true;
l = 1;
h = n - 1;
// add the following 2 lines is OK.
while (A[l] == base) ++l;
while (A[h] == base) --h;
while (l <= h) {
mid = (h -l) / 2 + l;
if (target == A[mid]) return true;
if (target < base) { // 先判断目标值与基准值的大小关系
// 判断中间值与基准值的大小关系
if (A[mid] > base) {l = mid + 1;}
else { // 目标值与中间值在同一类区间段时,就是普通的二分查找了
if (target < A[mid]) h = mid - 1;
else l = mid + 1;
}
} else {
if (A[mid] < base) {h = mid - 1;}
else {
if (target < A[mid]) h = mid - 1;
else l = mid + 1;
}
}
}
return false;
}
};