153. Find Minimum in Rotated Sorted Array

Back to Homepage   |     Back to Code List


class Solution {
    public int findMin(int[] nums) {
        int n = nums.length;
        if (nums[0] < nums[n - 1]) return nums[0];
        int lo = 0, hi = n - 1; 
        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            if (mid > 0 && nums[mid] < nums[mid - 1]) {
                return nums[mid];
            }
            if (nums[mid] > nums[mid + 1]) {
                return nums[mid + 1];
            }
            if (nums[lo] < nums[mid]) { 
                lo = mid + 1;
            } else {
                hi = mid - 1;
            }
        }
        
        return nums[lo];
    }
}