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 相比的话,有了相等的数,那么不能使用第一个数和最后一个数相比来确定第一个数就是最小数的方法,只能是从左向右遍历,遍历到一个降序,那么这个开始降序的元素就是最小值,如果遍历整个过程中找不到,那么第一个元素就是最小值
public class Solution { public int findMin(int[] num) { for(int i = 1; i < num.length; i++){ if(num[i] < num[i-1]){//降序 return num[i]; } } return num[0]; } }
Runtime: 212 ms
神马个情况,难道是我的网速快了,第一次写出这么快代码,低调,或许真的是自己的网速快了,淡定