81. Search in Rotated Sorted Array II
参考大佬题解,如果大家有兴趣的话可以看看,博客里说的很清楚,放上链接,渣渣我就不做过多解释了。以下是大佬博文链接:Search in Rotated Sorted Array II – LeetCode 最后放上渣渣我写的代码,供参考:
class Solution {
public:
bool search(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
int mid = (left + right) / 2;
while (left <= right) {
mid = (left + right) / 2;
if (target == nums[mid])
return true;
if (nums[left] < nums[mid]) {
if (target < nums[mid] && target >= nums[left])
right = mid - 1;
else
left = mid + 1;
} else if (nums[mid] < nums[left]) {
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
} else
right = mid - 1;
} else {
left++;
}
}
return false;
}
};