Find Minimum in Rotated Sorted Array II
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.
和上一题Find Minimum in Rotated Sorted Array一样的思路。
1 class Solution { 2 public: 3 int findMin(vector<int>& nums) { 4 if(nums.size()==0) return 0; 5 int left=0,right=nums.size()-1; 6 while(left<right && nums[left]>nums[right]) 7 { 8 int mid = (left+right)/2; 9 if(nums[mid]>nums[right]) 10 { 11 left = mid+1; 12 } 13 else 14 { 15 right = mid; 16 } 17 } 18 return nums[left]; 19 } 20 };