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.
Array Binary Search
Have you met this question in a real interview?
思路:二叉搜索
该问题没有重复的节点
class Solution {
public:
int findMin(vector<int> &num) {
int l=0;
int r=num.size()-1;
int res=0;
while(l<=r)
{
if(num[l]<=num[r])//这里等号是处理l==r的情况
{
res=num[l];
break;
}
int m=l+(r-l)/2;
if(num[m]>=num[l])
l=m+1;
else
r=m;
}
return res;
}
};