题解 | #旋转数组的最小数字#
旋转数组的最小数字
http://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba
这是简单题嘛,好难呜呜呜~~~~~~,二分查找的变种,规划end(最后的下标)与mid(中间的下标)
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
int mid = array.length / 2;
int first = 0;
int end = array.length-1;
int w = 0;
while(first < end){
if((end - first) == 1){
break;
}
if(array[end] > array[mid]){
end=mid;
mid = (end+first) / 2;
}
else if(array[end] < array[mid]){
first = mid+1; //最小的不会是mid,还有end小
mid = (end+first) / 2;
}
else if(array[end] == array[mid]){
end--;
mid = (end+first) / 2;
}
}
if(array[first] > array[end]){
return array[end];
}
else{
return array[first];
}
}
}