每次都与nums[0]比较,若nums[mid]>nums[0]说明最小值在mid右边
class Solution {
public:
int findMin(vector<int>& nums) {
int n=nums.size();
if(n==1)
return nums[0];
int l=0,r=n-1;
int target=nums[0];
while(l<=r)
{
int mid=(l+r)>>1;
if(nums[mid]<nums[0])
{
target=min(target,nums[mid]);
r=mid-1;
}
else
{
l=mid+1;
}
}
return target;
}
};
update
class Solution {
public:
int findMin(vector<int>& nums) {
int n=nums.size();
if(n==1)
return nums[0];
int l=0,r=n-1;
while(l<r)
{
int mid=(l+r)>>1;
if(mid<n-1&&nums[mid]>nums[mid+1])
return nums[mid+1];
else if(nums[mid]<nums[l])
r=mid;
else if(nums[mid]>nums[r])
l=mid;
else
return nums[l];
}
return nums[l];
}
};