通用解法:
情况一:
情况二:
情况三:
package test;
import java.util.ArrayList;
class Solution {
public int minNumberInRotateArray(int [] array) {
//二分查找
//边界条件
if (array == null || array.length == 0) {
return 0;
}
//二分查找框架
int left = 0;
int right = array.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (array[mid] < array[right]) {
right = mid;
} else if (array[mid] > array[right]){
left = mid + 1;
} else {
right--;
}
}
return array[left];
}
}
public class Main{
public static void main(String[] args) {
Solution solution = new Solution();
// int []a = {1,0,1,1,1};
int []a = {4,5,6,7,8,1,2,3};
int b = solution.minNumberInRotateArray(a);
System.out.print("最小值是:" + b);
}
}
我的解法(小白时候的暴力):
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length == 0){
return 0;
}
int low = 0;
int high = array.length-1;
while(low < high){
if(array[low] < array[high]){
return array[low];
}else
low++;
}
return array[low];
}
}