https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/
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 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.
The array may contain duplicates.
package go.jacob.day815;
/**
* 154. Find Minimum in Rotated Sorted Array II
*
* @author Jacob
*
*/
public class Demo3 {
public int findMin(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
//如果nums[left] > nums[mid],说明存在left,mid降序
if (nums[left] > nums[mid]){
left ++;
right=mid;
}
//如果nums[mid] > nums[right],说明存在mid,right降序
else if (nums[mid] > nums[right]) {
left=mid+1;
} else
//不存在降序,说明有重复元素,把数组长度缩短1;
right--;
}
return nums[left];
}
}