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.
You may assume no duplicate exists in the array.
public class Solution {
public int findMin(int[] num) {
int len = num.length;
for(int i = 0;i < len ; i++){
if(num[i] < num[(i-1+len)%len]){
return num[i];
}
}
return num[0];//长度为1时
}
}
还是比较简单的,想象成一个环,然后找到一个降序的位置就一定是最小的位置
Runtime: 209 ms