问题描述如下:
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.
问题链接cpp代码如下:
class Solution {
public:
int findMin(vector<int> &num) {
if(num.size()==0)return -1;
int l=0,h=num.size()-1;
while(num[l]>num[h]){
if(l+1==h)
return num[h];
int m=l+(h-l)/2;
if(num[l]<num[m])
l=m;
else h=m;
}
return num[l];
}
};