首页 > 代码库 > leetcode-Search in Rotated Sorted Array II-81

leetcode-Search in Rotated Sorted Array II-81

这题和http://www.cnblogs.com/0summer/p/5825282.html一样,只是上一题中序列里没有重复的元素

代码都是一样的

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

 

leetcode-Search in Rotated Sorted Array II-81