Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 672 Bytes

153. Find Minimum in Rotated Sorted Array.md

File metadata and controls

24 lines (21 loc) · 672 Bytes
// compare the mid with the nums[right]
class Solution {
public:
    int findMin(vector<int>& nums) {
      	// be aware of the right, if right is nums.size(), it will overflow the array
        int left = 0, right = nums.size() - 1;
        while (left < right) {
          	// if the sub array is not rottated, return the first element.
            if (nums[left] < nums[right]) return nums[left];
            
            int mid = left + (right - left) / 2;
            
            if (nums[mid] < nums[right]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return nums[left];
    }
};