---
Q:
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
A:
分类: binary search。当前 146 cases,4ms。
Q:
Suppose an array sorted in ascending order 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.
A:
分类: binary search。当前 146 cases,4ms。
class Solution {
public:
int findMin(vector<int>& nums, int l, int r)
{
if (l >= r)
{
return nums[r];
}
if (nums[l] <= nums[r])
{
return nums[l];
}
else
{
int m = (l + r) >> 1;
if (nums[m] >= nums[l])
{
return findMin(nums, m + 1, r);
}
else
{
return findMin(nums, l, m);
}
}
}
int findMin(vector<int>& nums) {
return findMin(nums, 0, nums.size() - 1);
}
};