LeetCode *** 153. Find Minimum in Rotated Sorted Array

来源:互联网 发布:step文件打开软件 编辑:程序博客网 时间:2024/06/05 00:57

题目:

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.


分析:

想感叹一下,难度同样都是medium,为毛题和题之间的差距这么大呢。。唉。。


代码:

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

0 0