154. Find Minimum in Rotated Sorted Array II

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 (nums[hi] < nums[mid]) { 
                lo = mid + 1;
            } else if (nums[mid] < nums[hi]) { 
                hi = mid;
            } else { 
                hi--;
            }
        }
        return nums[lo];
    }
}