Easy Java Solution


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

Log in to reply
 

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