- 题目描述
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
请找出其中最小的元素。
注意数组中可能存在重复的元素。
- 题解
二分查找
- 代码
class Solution {
public:
int findMin(vector<int>& nums) {
int n=nums.size();
int l=0,h=n-1;
while (l<h){
int m=l+((h-l)>>1);
if (nums[m]>nums[h]) l=m+1;
else if (nums[m]<nums[h]) h=m;
else h--;
}
return nums[l];
}
};