Find Minimum in Rotated Sorted Array I, II

之前一步一步存的方法略复杂啊,其实直接二分扔就可以咯

这里直接给个II的解法,和I多个冗余挪位的判断

public class Solution {

    public int findMin(int[] nums) {

        int min = nums[0];

        int l=0, h = nums.length-1;

        while(l<=h){

            int m = (l+h)/2;

            if(nums[m]<nums[h]){

                h =m-1;

            }else if(nums[m]>nums[h]){

                l = m+1;

            }else{

            h--;

            }

            min = Math.min(min,nums[m]);

        }

        return min;

    }

}

 

你可能感兴趣的:(array)