easy to understand,a little change base on the first question(java)


  • 0
    M

    when val of mid equals to the val of end,there is a case that the min val occurs between the mid and end,such as 3,3,3,1,3,so just a liitle change based on the first question without duplicates

    public class Solution {
        public int findMin(int[] nums) {
            if(nums.length==1)return nums[0];
            int left=0;
            int right=nums.length-1;
            int mid=0;
            while(left<right-1){
                mid=left+(right-left)/2;
                while(mid<right&&nums[right]==nums[mid])right--;
                if(nums[mid]<=nums[right]){
                    right=mid;
                }else{
                    left=mid;
                }
            }
            return Integer.min(nums[left],nums[right]);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.