Find Minimum in Rotated Sorted Array
另外一个简洁一点的解法是:public class Solution {
public int findMin(int[] num) {
if(num == null || num.length == 0) return 0;
int low = 0, high = num.length - 1;
while(low < high) {
if(low == high - 1 && num[low] > num[high]) return num[high];
int mid = low + (high - low) / 2;
if(num[low] < num[mid]) {
low = mid;
} else {
high = mid;
}
}
return num[0];
}
}
public class Solution {
public int findMin(int[] num) {
int low = 0, high = num.length - 1;
while(low < high && num[low] > num[high]) {
int mid = low + (high - low) / 2;
if(num[low] <= num[mid]) { //因为low可能等于mid,所以要考虑等于的情况
low = mid + 1;
} else {
high = mid;
}
}
return num[low];
}
}

浙公网安备 33010602011771号