Leetcode:153. 寻找旋转排序数组中的最小值

class Solution {
    public int findMin(int[] nums) {
        int start=0;
        int end=nums.length-1;
        int mid=0;
        while(start<end){
            mid=start+(end-start)/2;
            //只使用相邻的mid和mid+1进行判断是不够的,比如[2,3,4,5,1]中,你会先在4的附近进行判断,发现是个递增就一直往前走,但忽略了最后的最小值,所以使用end可以把所有元素考虑进来
            if(nums[mid]<nums[end]){
                end=mid;
            }
            else{
                start=mid+1;
            }
        }
        return nums[start];
    }
}
posted @ 2022-03-13 10:01  Dreamer_szy  阅读(22)  评论(0)    收藏  举报