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

LeetCode-153-Find Minimum in Rotated Sorted Array

时间:2019-02-07 10:52:51      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:you   out   mini   end   minimum   element   color   div   注意   

算法描述:

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]).

Find the minimum element.

You may assume no duplicate exists in the array.

Example 1:

Input: [3,4,5,1,2] 
Output: 1

Example 2:

Input: [4,5,6,7,0,1,2]
Output: 0

解题思路:二分法,注意细节。

        if(nums.size()==1) return nums[0];
        int left = 0;
        int right = nums.size()-1;
        while(left < right){
            if(nums[left] < nums[right]) return nums[left];
            int mid = left + (right - left) / 2;
            if(nums[mid] > nums[right]){
                left = mid +1;
            } else
                right = mid;
        }
        return nums[left];
    }

 

LeetCode-153-Find Minimum in Rotated Sorted Array

标签:you   out   mini   end   minimum   element   color   div   注意   

原文地址:https://www.cnblogs.com/nobodywang/p/10354523.html

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