# 搜索旋转排序数组 II

已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。

在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转 ,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,4,4,5,6,6,7] 在下标 5 处经旋转后可能变为 [4,5,6,6,7,0,1,2,4,4]

给你 旋转后 的数组 nums 和一个整数 target ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 nums 中存在这个目标值 target ,则返回 true ,否则返回 false

 

示例 1:

输入:nums = [2,5,6,0,0,1,2], target = 0
输出:
true

示例 2:

输入:nums = [2,5,6,0,0,1,2], target = 3
输出:
false

 

提示:

 

进阶:

以下错误的选项是?

## aop ### before ```cpp #include using namespace std; ``` ### after ```cpp int main() { Solution sol; vector nums = {2, 5, 6, 0, 0, 1, 2}; int target = 0; int res; res = sol.search(nums, target); cout << res << endl; return 0; } ``` ## 答案 ```cpp class Solution { public: bool search(vector &nums, int target) { if (nums.empty() || nums.size() == 0) { return false; } int start = 0; int end = nums.size() - 1; int mid; while (start <= end) { mid = start + (end - start) / 2; if (nums[mid] == target) { return true; } if (nums[start] < nums[mid]) { if (nums[mid] > target && nums[start] <= target) { end = mid - 1; } else { start = mid + 1; } } else { if (nums[mid] < target && nums[end] >= target) { start = mid + 1; } else { end = mid - 1; } } } return false; } }; ``` ## 选项 ### A ```cpp class Solution { public: bool search(const vector &nums, int target) { int first = 0, last = nums.size(); while (first != last) { const int mid = first + (last - first) / 2; if (nums[mid == target]) { return true; } if (nums[first] < nums[mid]) { if (nums[first] <= target && target < nums[mid]) { last = mid; } else { first = mid + 1; } } else if (nums[first] > nums[mid]) { if (nums[mid] < target && target <= nums[last - 1]) { first = mid + 1; } else { last = mid; } } else { first++; } } return false; } }; ``` ### B ```cpp class Solution { public: bool search(vector &nums, int target) { if (nums.empty()) return false; int i = 0, j = nums.size() - 1, mid; mid = (i + j) / 2; if (nums[mid] == target) return true; else if (mid != i && nums[i] == nums[mid]) { i = mid + 1; while (i <= j && nums[i] == nums[mid]) i++; if (i == nums.size()) { i = 0; j = mid - 1; while (i <= j && nums[j] == nums[mid]) j--; } } while (i <= j) { mid = (i + j) / 2; if (nums[mid] == target) return true; else if (nums[mid] >= nums[i]) { if (target >= nums[i] && target < nums[mid]) { j = mid - 1; while (i <= j && nums[j] == nums[mid]) j--; } else { i = mid + 1; while (i <= j && nums[i] == nums[mid]) i++; } } else { if (target > nums[mid] && target <= nums[j]) { i = mid + 1; while (i <= j && nums[i] == nums[mid]) i++; } else { j = mid - 1; while (i <= j && nums[j] == nums[mid]) j--; } } } return false; } }; ``` ### C ```cpp class Solution { public: bool search(vector &nums, int &target) { int n = nums.size(); if (n == 0) return false; int left = 0, right = n - 1; while (left <= right) { int mid = (left + right) / 2; if (nums[mid] == target) return true; else if (nums[mid] < nums[right]) { if (nums[mid] < target && nums[right] >= target) left = mid + 1; else right = mid - 1; } else if (nums[mid] > nums[right]) { if (nums[left] <= target && nums[mid] > target) right = mid - 1; else left = mid + 1; } else --right; } return false; } }; ```