My java solution with binary search


  • 0
    L
    public class Solution {
    public int findMin(int[] nums) {
        int n=nums.length;
        if(n<1) return 0;
        if(n<2) return nums[0];
        if(n<3) return Math.min(nums[0],nums[1]);
        if(nums[0]<nums[n-1]) return nums[0];
        int a=0,b=n-1;int mid=(a+b)/2;
        while(a<b) {
            if(nums[mid]>nums[a] && nums[mid]>nums[b]){
                a=mid;
                mid=(a+b)/2;
            }
            else if(nums[mid]<nums[a] && nums[mid]<nums[b]){
                b=mid;
                mid=(a+b)/2;
            }
            if(mid==a||mid==b) return Math.min(nums[a],nums[b]);
        }
        return 0;
    }
    

    }


Log in to reply
 

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