与33 搜索排序旋转数组的区别就是加上了重复元素的跳过
bool search(vector<int>& nums, int target) {
if (nums.size() == 0)
return false;
int l = 0, r = nums.size() - 1;
while (l < r) {
while(l <= nums.size() - 2 && nums[l] == nums[l + 1])
l++;
while(r >= 1 && nums[r] == nums[r - 1])
r--;
int mid = (l + r) / 2;
if (nums[mid] > nums[l])
l = mid ;
else
r = mid;
}
int split = l;
if (target <= nums[split] && target >= nums[0]) {
r = split;
l = 0;
while (l < r) {
int mid = (l + r) / 2;
if (nums[mid] < target)
l = mid + 1;
else
r = mid;
}
if (nums[l] == target)
return true;
else
return false;
}
else {
r = nums.size() - 1;
l = split + 1;
while (l < r) {
int mid = (l + r) / 2;
if (nums[mid] < target)
l = mid + 1;
else
r = mid;
}
if (l < nums.size() && nums[l] == target)
return true;
else
return false;
}
}