码迷,mamicode.com
首页 > 其他好文 > 详细

154. Find Minimum in Rotated Sorted Array II

时间:2020-03-21 14:54:19      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:log   htm   code   color   com   https   方法   span   ted   

https://www.cnblogs.com/habibah-chang/p/12538877.html

附加条件,允许重复数值。

Example 1:
Input: [1,3,5]
Output: 1

Example 2:
Input: [2,2,2,0,1]
Output: 0

  

方法:

思想同上述链接题目,二分查找

附加处理:

如果mid==low的值,那么不确定low~mid都是一样的值,还是其中存在最小值。

那么,只能high--,一项一项移动来确定。

 

参考代码:

 1 class Solution {
 2 public:
 3     int findMin(vector<int>& nums) {
 4         int low=0, high=nums.size()-1;
 5         while(low<high){
 6             if(nums[low]<nums[high]) return nums[low];
 7             int mid=(low+high)/2;
 8             if(nums[mid]>=nums[low]){
 9                 if(nums[mid]==nums[high]) high--;//★
10                 else low=mid+1;
11             }else{
12                 high=mid;
13             }
14         }
15         return nums[low];
16     }
17 };

 

154. Find Minimum in Rotated Sorted Array II

标签:log   htm   code   color   com   https   方法   span   ted   

原文地址:https://www.cnblogs.com/habibah-chang/p/12538919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!