binary search solution(java)


  • 0
    M

    use the binary search to keep the array always being rotated, so the min is alway in the left array till there is only one element.

    public class Solution {
        public int findMin(int[] nums) {
            int left=0;
            int right=nums.length-1;     
            while(left<right){
                int mid=left+(right-left)/2;
                if(nums[mid]>nums[right]){  //mid is possible equal to mid
                    left=mid+1;
                }else{
                    right=mid;  //mid could the the final result ,so mid-1 is wrong
                }
            }
            return nums[left];
        }
    }
    

Log in to reply
 

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