154. Find Minimum in Rotated Sorted Array II
Suppose a sorted array 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.
The array may contain duplicates.
class Solution {
public:
int findMin(vector<int>& nums) {
int lb = 0;
int ub = nums.size() - 1;
while(ub > lb){
int mid = (ub - lb)/2 + lb;
if(nums[mid] > nums[ub])
lb = mid + 1;
else if(nums[mid] < nums[ub])
ub = mid;
else{
ub--;
}
}
return nums[ub];
}
};
class Solution {
public:
int findMin(vector<int>& nums) {
int lb = 0;
int ub = nums.size() - 1;
while(lb < ub){
if(nums[lb] < nums[ub]) return nums[lb];
else{
int mid = (ub - lb)/2 + lb;
if(nums[mid] > nums[lb])
lb = mid + 1;
else if(nums[mid] < nums[lb])
ub = mid;
else{
lb++;
}
}
}
return nums[lb];
}
};