class Solution {
public:
int findMin(vector<int>& nums) {
if (nums.size() == 0) {
return 0;
}
int size = nums.size(), left = 0, right = size - 1;
if (nums[left] > nums[right]) {
while (left != (right - 1)) {
int mid = (left + right) / 2;
if (nums[left] < nums[mid]) {
left = mid;
} else {
right = mid;
}
}
return min(nums[left], nums[right]);
}
return nums[0];
}
};
class Solution {
public:
int findMin(vector<int>& nums) {
if (nums.empty()) return 0;
return search(nums, nums[0], 0, nums.size() - 1);
}
private:
int search(vector<int>& nums, int res, int left, int right) {
if (left < right - 1) {
int mid = (left + right) / 2;
if (nums[left] < nums[mid]) {
res = min(res, nums[left]);
return search(nums, res, mid, right);
} else {
res = min(res, nums[right]);
return search(nums, res, left, mid);
}
}
res = min(res, nums[left]);
res = min(res, nums[right]);
return res;
}
};