Skip to content

Commit 44796e4

Browse files
Create search_in_rotated_sorted_array_ii.cpp
1 parent eaadbc1 commit 44796e4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

search_in_rotated_sorted_array_ii.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
bool search(vector<int>& nums, int target) {
4+
5+
if(nums.size() == 0) return false;
6+
7+
int mid, left = 0, right = nums.size() - 1;
8+
9+
while(left <= right) {
10+
11+
mid = left + (right - left) / 2;
12+
13+
if(nums[mid] == target) return true;
14+
15+
if(nums[mid] == nums[left] and nums[mid] == nums[right]) {
16+
left++, right--;
17+
}
18+
else if(nums[left] <= nums[mid]) {
19+
if(nums[left] <= target and target < nums[mid]) {
20+
right = mid - 1;
21+
}
22+
else left = mid + 1;
23+
}
24+
else {
25+
if(nums[mid] < target and target <= nums[right]) {
26+
left = mid + 1;
27+
}
28+
else right = mid - 1;
29+
}
30+
}
31+
return false;
32+
}
33+
};

0 commit comments

Comments
 (0)