【LeetCode】153. Find Minimum in Rotated Sorted Array

Suppose a sorted array 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.

二分查找

class Solution {
public:
    int findMin(vector<int>& nums) {
        int low = 0;
        int high = nums.size()-1;
        int result;
        int mid;
        while(low < high)
        {
            mid = low + (high-low)/2;
             if(nums[low] < nums[high]) return nums[low];  
            else {  
                if(nums[low] <= nums[mid]) low = mid + 1;  
                else high = mid;  
            }  
        }
        return nums[low];
    }
};


你可能感兴趣的:(【LeetCode】153. Find Minimum in Rotated Sorted Array)