JAVA Both Iterative and Recursive 1ms AC solution


  • 0
    L

    Recursive Solution:

     public int findMin(int[] nums) {
           return findMin(nums, 0, nums.length - 1);
        }
        public int findMin(int[] nums, int low, int high){
            if(low == high) return nums[low];
            int mid = (low + high)/2;
            if(nums[mid] > nums[high]){
                return findMin(nums, mid+1, high);
            }
            else
                return findMin(nums,low, mid);
        }
    

    Iterative Solution:

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

Log in to reply
 

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