Find Minimum in Rotated Sorted A
2017-03-04 本文已影响0人
pretzei
地址:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
题意为旋转数组中的最小数
剑指offer里有,所以很快就解决了。二分
int findMin(vector<int>& nums) {
int left = 0, right = nums.size()-1;
while (left < right) {
if (nums[left] < nums[right]) {
return nums[left];
}
int mid = (left + right) >> 1;
if (nums[mid] >= nums[left]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}