/**
* 旋转数组的最小数字(寻找右排序的首个元素x,x也可以称为旋转点)
* @Description
*/
public class Test09 {
public int minArray(int[] numbers) {
// int i = 0, j = numbers.length - 1;
// while (i < j) {
// int m = (i + j) / 2;
// if (numbers[m] > numbers[j]) i = m + 1;
// else if (numbers[m] < numbers[j]) j = m;
// else j--;
// }
// return numbers[i];
//剑指offer思路,超出时间限制
if (numbers == null || numbers.length <= 0){
return -1;
}
int left = 0;
int right = numbers.length - 1;
int mid = left;//mid为旋转数组的旋转点
while (numbers[left] >= numbers[right]){
if (right - left == 1) {
mid = right;
break;
}
mid = (left + right) / 2;
if (numbers[mid] >= numbers[left]){
left = mid;
}else if (numbers[mid] <= numbers[right]){
right = mid;
}
}
return numbers[mid];
}
public static void main(String[] args) {
int[] numbers = new int[]{3,4,5,6,7,8,1,2};
System.out.println(new Test09().minArray(numbers));
}
}
剑指Offer-08-旋转数组的最小数字
最新推荐文章于 2020-09-01 18:55:07 发布