LeetCode153. 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.

    int findMin(vector<int>& nums) {
        if (nums.size() == 1 || nums[0] < nums.back())
            return nums[0];
        int i = nums.size()-1;
        while(nums[i] > nums[i-1]) i--;
        return nums[i];
    }

我的LeetCode代码都在github上,欢迎大家关注

https://github.com/booirror/LeetCode-cpp



你可能感兴趣的:(LeetCode,数组)