Java binary search solution

  • 0
    public int findMin(int[] nums) {
        return binarySearch(nums,0,nums.length-1);
    private int binarySearch(int[] nums,int l,int h){
        if(l == h) return nums[l];
        if(l == h-1) return Math.min(nums[l],nums[h]);
        if(nums[l] < nums[h]) return nums[l];
        int m = (l+h)/2;
        if(nums[m] < nums[l] && nums[m] < nums[h]) return binarySearch(nums,l,m); 
        else return binarySearch(nums,m,h);

Log in to reply

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