首页 > 代码库 > 剑指offer java -查找旋转数组的最小数字

剑指offer java -查找旋转数组的最小数字

/**
* Created by wqc on 2017/7/18.
* 查找旋转数组的最小数字
* 把一个数组最开始的若干个元素搬到数组的末尾,称为数组的旋转
* 输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素
* 如:3,4,5,1,2 为1,2,3,4,5的一个旋转,最小值为1
*/
public class Problem8_findMinNumber {
public Integer findMinNum(int[] array)
{
if(array == null)
{
return null;
}

int leftIndex = 0;
int rightIndex =array.length-1;
int mid = 0;
while(array[leftIndex]>=array[rightIndex])
{
if(rightIndex-leftIndex <= 1)
{
mid = rightIndex;
break;
}

mid = (leftIndex + rightIndex)/2;
if(array[leftIndex] == array[rightIndex] && array[leftIndex] == array[mid])
{
if(array[leftIndex + 1] != array[rightIndex - 1])
{
mid = array[leftIndex + 1] < array[rightIndex -1] ? (leftIndex + 1) : (rightIndex - 1);
break;
}else{
leftIndex++;
rightIndex--;
}
}else if(array[mid] >= array[leftIndex]){

leftIndex = mid;
}else if(array[mid] <= array[rightIndex]){

rightIndex = mid;
}
}
return array[mid];
}

}

剑指offer java -查找旋转数组的最小数字