public class E11RotateInOrderNumbers {
//寻找某数组旋转数组中最小数字
//特殊情况:移动0个元素;数组中存在重复的数字
public static int getMin(int[] numbers, int length){
if (numbers == null || length <= 0)
throw new IllegalArgumentException("Invalid Input");
int startIndex = 0;
int endIndex = length - 1;
int midIndex = startIndex;
while(numbers[startIndex] >= numbers[endIndex]){
//循环终止条件
if (endIndex == startIndex + 1)
return numbers[endIndex];
//三个指针相等时遍历数组寻找
if (numbers[startIndex] == numbers[midIndex]
&& numbers[midIndex] == numbers[endIndex])
return findMin(numbers, startIndex, endIndex);
midIndex = (startIndex + endIndex) / 2;
if (numbers[midIndex] >= numbers[startIndex])
startIndex = midIndex;
else if (numbers[midIndex] <= numbers[endIndex])
endIndex = midIndex;
}
//移动0个元素直接返回数组第一个数字
return midIndex;
}
private static int findMin(int[] numbers, int startIndex, int endIndex){
//遍历寻找
int minNumber = numbers[startIndex];
for (int i = startIndex; i <= endIndex; i++){
if (numbers[i] < minNumber)
minNumber = numbers[i];
}
return minNumber;
}
}