AC Java code using binary search


  • 0
    J
    public class Solution {
        public int findMin(int[] nums) {
            if(nums.length==0) return 0;
            if(nums[0]<=nums[nums.length-1]) return nums[0];
            int start=0;
            int end=nums.length-1;
            while(start+1<end){
                int mid=start+(end-start)/2;
                if(nums[mid]<nums[mid-1]&&nums[mid]<nums[mid+1]){
                    return nums[mid];
                }else if(nums[mid]>nums[0]){
                    start=mid;
                }else{
                    end=mid;
                }
            }
            return nums[nums.length-1];
            
        }
    }

Log in to reply
 

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