[leetCode]剑指 Offer 11. 旋转数组的最小数字

在这里插入图片描述

class Solution {
    public int minArray(int[] numbers) {
        if( numbers == null || numbers.length == 0) return -1; 
        int lo = 0, hi = numbers.length - 1;
        int mid = lo; //考虑旋转0个元素的特例
        while( numbers[lo] >= numbers[hi] ) {
            mid = (hi - lo)/2 + lo;
            if( lo + 1 == hi){
                mid = hi;
                break ;
            }
            //考虑三个位置元素相等的情况,此时只能顺序查找
            if(numbers[lo] == numbers[hi] && numbers[lo] == numbers[mid]){
                return min(numbers, lo, hi);
            }
            if(numbers[mid] >= numbers[lo]){
                lo = mid;
            }else if(numbers[mid] <= numbers[hi]){
                hi = mid;
            }
        }
        return numbers[mid];
    }

    private int min(int[] a,int lo, int hi){
        int min = a[lo];
        for(int i = lo + 1; i <= hi; i++){
            if(a[i] < a[lo]){
                min = a[i];
            }
        }
        return min;
    }
}
posted @ 2020-08-06 10:35  消灭猕猴桃  阅读(43)  评论(0编辑  收藏  举报