1. 程式人生 > >153. Find Minimum in Rotated Sorted Array(二分法)

153. Find Minimum in Rotated Sorted Array(二分法)

題目描述:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/

二分法,如果某一半是單增的,那麼最小值一定在另一半或者這一半的頭位置。

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