思路:搜索问题中,综合效率最高的是二分算法,利用二分算法写好,测试后,发现多个相同数据会导致指针移动出错。于是加上条件:两边指针一致时一起移动来压缩搜索空间,解决问题
public class Solution {
public int minNumberInRotateArray(int [] array) {
int low=0;
int high = array.length-1;
int ans = array[0];
while(low<=high){
int mid = (low+high)/2;
if(array[mid]<ans){
ans = array[mid];
}
if(array[low] == array[high]){
low++;
high--;
}else if(array[mid]>array[high]){
low=mid+1;
}else{
high=mid-1;
}
}
return ans;
}
}