当前位置: 代码迷 >> 综合 >> Leetcode 81. Search in Rotated Sorted Array II
  详细解决方案

Leetcode 81. Search in Rotated Sorted Array II

热度:10   发布时间:2023-12-12 21:23:57.0

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Search in Rotated Sorted Array II

2. Solution

class Solution {
public:bool search(vector<int>& nums, int target) {int size = nums.size();int left = 0;int right = size - 1;while(left <= right) {int mid = (left + right) / 2;if(nums[mid] == target) {return true;}if(nums[left] == nums[mid]) {left++;continue;}if(nums[left] < nums[mid]) {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;}
};

Reference

  1. https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/
  相关解决方案