Find Minimum in Rotated Sorted Array

LeetCode Q 153 - Find Minimum in Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
You may assume no duplicate exists in the array.

Solution

Code:

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

   Reprint policy


《Find Minimum in Rotated Sorted Array》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC