方法一:利用旋转数组部分单调递增的特点。
class Solution {
public int minArray(int[] numbers) {
if (numbers.length == 1)
return numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < numbers[i - 1])
return numbers[i];
}
return numbers[0];
}
}
方法二:二分法,基于二分查找法的变形,遇到单调递增或递减的数组都要第一时间想到二分查找法。
class Solution {
public int minArray(int[] numbers) {
int low = 0;
int high = numbers.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (numbers[mid] < numbers[high])
high = mid;
else if (numbers[mid] > numbers[high])
low = mid + 1;
else
high -= 1;
}
return numbers[low];
}
}