1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public: bool search(vector<int>& nums, int target) { int n = nums.size(); if (n == 1) return nums[0] == target; int left = 0, right = n - 1; while (left <= right) { int mid = (right + left) / 2; if (nums[mid] == target) return true; if (nums[left] == nums[mid]) { ++left; continue; } if (nums[mid] > nums[left]) { if (target >= nums[left] && target < nums[mid]) right = mid - 1; else left = mid + 1; } else { if (target > nums[mid] && target <= nums[right]) left = mid + 1; else right = mid - 1; } } return false; } };
|