旋转数组找元素,其中元素不唯一
可以延续上一题
我们依然要在有序的部分里面找
如果 n u m s [ l ] = n u m s [ m i d ] = n u m s [ r ] nums[l] = nums[mid]=nums[r] nums[l]=nums[mid]=nums[r]那我们也是啥都不知道,那只能一个个搜了
class Solution {
public:bool search(vector<int>& nums, int target) {int l = 0, r = nums.size() - 1;while(l <= r){int mid = l + (r - l) / 2;if(nums[mid] == target)return true;if(nums[l] == nums[mid] && nums[mid] == nums[r]){++l;--r;}else if(nums[l] <= nums[mid]){if(nums[l] <= target && target < nums[mid]){r = mid - 1;}else{l = mid + 1;}}else{if(nums[mid] < target && target <= nums[r]){l = mid + 1;}else{r = mid - 1;}}}return false;}
};