Find Minimum in Rotated Sorted Array II
Total Accepted: 52722
Total Submissions: 152155
Difficulty: Hard
Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?Would this affect the run-time complexity? How and why?
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.
Subscribe to see which companies asked this question
Hide Similar Problems
思路:
承接上题【Find Minimum in Rotated Sorted Array】,当有重复元素时,退化成顺序寻找。
c++ code:
class Solution {
public:
int findMin(vector<int>& nums) {
int lo = 0,hi = nums.size()-1;
while(lo < hi) {
if(nums[lo] < nums[hi]) return nums[lo];
int mid = lo + (hi-lo)/2;
if(nums[lo] == nums[mid] && nums[mid] == nums[hi]) {
lo++;
hi--;
} else if(nums[lo] <= nums[mid])
lo = mid + 1;
else
hi = mid;
}
return nums[lo];
}
};