LeetCode153. Find Minimum in Rotated Sorted Array如何用五行代码解决它

来源:互联网 发布:怎么增加淘宝搜索引擎 编辑:程序博客网 时间:2024/05/16 14:08

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



0 0
原创粉丝点击