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.
Have you met this question in a real interview?
Yes
No
二分查找,不过要比较的是num[len-1]这个元素,找到比num[len-1]小的元素一直缩进即可
class Solution {
public:
int findMin(vector<int> &num) {
int len = num.size();
if(len==0) return 0;
int left = 0, right = len-1;
while(left<right){
int mid = (left+right)/2;
if(num[mid]>num[len-1])
left = mid+1;
else
right = mid;
}
return num[left];
}
};