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.
思路:用binarysearch,设置数组最后一个数为pivot,每次mid与pivot比较。
c++代码如下:
class Solution {
public:
int findMin(vector<int> &num) {
int low = 0;
int high = num.size() - 1;
int pivot = high;
while(low <= high){
int mid = (low + high)>>1;
if(num[mid] > num[pivot])
low = mid + 1;
else
high = mid - 1;
}
return num[low];
}
};