leetcode-Find Minimum in Rotated Sorted Array II-154

Posted 0_summer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

leetCode-Find All Numbers Disappeared in an Array

LeetCode-Find the Celebrity

LeetCode-Find the Duplicate Number

LeetCode-Find Median from Data Stream

leetcode-Find Peak Element-162

153. Find Minimum in Rotated Sorted Array