1、二分法求旋转数组的最小数
/**
* 利用二分法求旋转数组的最小数:
*
* 3,4,5,1,2
* 1 0 1 1 1
* 1 1 1 0 1
*
* */
public static int getMin(int[] arr){
int start=0;
int end=arr.length-1;
boolean flag=false;
while(arr[start]>arr[end]){
int mid=(start+end)/2;
if(start+1==end){
return arr[end];
}
if(arr[mid]>arr[start]){
start=mid;
}else if(arr[mid]<arr[end]){
end=mid;
}
}
int mid=(start+end)/2;
while(arr[start]==arr[mid]){
if(start+1==mid){
break;
}
mid--;
}
while(arr[end]==arr[mid]){
if(end==mid+1){
break;
}
mid++;
}
return arr[mid];
}
本文介绍了一种使用二分法查找旋转排序数组中最小元素的方法。通过不断缩小搜索范围,该算法能在O(log n)的时间复杂度内找到最小值。文章详细解释了算法流程,并提供了一个具体的Java实现示例。
1566

被折叠的 条评论
为什么被折叠?



