My Java solution with binary search


  • 0
    S
    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 left = 0, right = nums.length -1;
            while(left < right) {
                int mid = (left + right) / 2;
                if(nums[left] < nums[right]) right = mid;
                else if(nums[mid] > nums[right]) left = mid+1;
                else if(nums[mid] < nums[left]) right = mid;
                else {
                    if(nums[mid] == nums[left]) left++;
                    else if(nums[mid] == nums[right]) right--;
                }
            }
            return nums[left];
        }
    }
    

Log in to reply
 

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