forked from timoncui/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch_in_Rotated_Sorted_Array_II.cpp
More file actions
39 lines (33 loc) · 1.06 KB
/
Search_in_Rotated_Sorted_Array_II.cpp
File metadata and controls
39 lines (33 loc) · 1.06 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
/*
Author: Timon Cui, [email protected]
Title: Search in Rotated Sorted Array II
Description:
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
Difficulty rating: Medium
Notes:
Worst case O(n) because sometimes by comparing target with A[M] we can eliminate only one element.
*/
class Solution {
public:
bool search(int A[], int n, int target) {
return search(A, 0, n - 1, target);
}
private:
bool search(int *A, int L, int R, int target) {
while (L <= R) {
int M = L + (R - L) / 2;
if (A[M] == target) return true;
if (A[M] > A[L]) { // Left part sorted
if (A[L] <= target && target <= A[M]) R = M - 1;
else L = M + 1;
} else if (A[M] < A[L]) { // Right part sorted
if (A[M] <= target && target <= A[R]) L = M + 1;
else R = M - 1;
} else return search(A, L, M - 1, target) || search(A, M + 1, R, target);
}
return false;
}
};