Skip to content

Commit 12e9707

Browse files
committed
3
1 parent 8c6741d commit 12e9707

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
bool search(int A[], int n, int target) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
int lf = 0, rt = n - 1;
8+
while (lf <= rt) {
9+
int m = lf + (rt - lf) / 2;
10+
if (A[m] == target)
11+
return true;
12+
13+
if (A[lf] < A[m]) {
14+
if (A[lf] <= target && target < A[m])
15+
rt = m - 1;
16+
else
17+
lf = m + 1;
18+
}
19+
else if (A[lf] > A[m]) {
20+
if (A[m] < target && target <= A[rt])
21+
lf = m + 1;
22+
else
23+
rt = m - 1;
24+
}
25+
else {
26+
bool onleft = search(A + lf + 1, m - lf - 1, target);
27+
if (onleft) return true;
28+
return search(A + m + 1, rt - m, target);
29+
}
30+
31+
}
32+
return false;
33+
}
34+
};

0 commit comments

Comments
 (0)