From : https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
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.
Hide Similar Problems
Solution :
class Solution {
public:
bool search(vector<int>& nums, int target) {
int start = 0, end = nums.size()-1;
while(start <= end) {
int mid = (start+end)>>1;
if(nums[mid] == target) {
return true;
}
if(nums[start] == nums[end]) {
break;
}
int midV=nums[mid], startV=nums[start], endV=nums[end];
if(midV>=startV && (target>midV || target<startV) || midV<startV && target<=endV && target>midV) {
start = mid+1;
} else {
end = mid-1;
}
}
if(nums[start] == nums[end]) {
for(int i=start; i<=end; i++) {
if(nums[i] == target) {
return true;
}
}
}
return false;
}
};