首页 > 代码库 > leetcode-Find Minimum in Rotated Sorted Array II-154

leetcode-Find Minimum in Rotated Sorted Array II-154

和上题一样,本题的序列中可能有重复的元素,不过用上题的代码能过

 1 class Solution { 2 public: 3     int findMin(vector<int>& nums) { 4         if(nums.size()==0) return 0; 5         int l=0,r=nums.size()-1; 6         int key=INT_MAX; 7         //int cnt=1; 8         while(l<=r){ 9             //cout<<cnt++<<endl;10             int mid=(l+r)>>1;11             if(nums[mid]<=key){12                 key=nums[mid];13             }14             if(nums[l]<nums[mid]){15                 if(nums[mid]>nums[r]) l=mid+1;16                 else r=mid-1;17             }18             else if(nums[l]>nums[mid]){19                 r=mid-1;20             }21             else l++;22         }23         return key;24     }25 };

 

leetcode-Find Minimum in Rotated Sorted Array II-154