zoukankan      html  css  js  c++  java
  • [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;
        }
    }
    
  • 相关阅读:
    命令模式
    装饰模式 decorator
    儒道佛
    Facade模式
    Adapter
    TListView ItemCheck CheckedItems
    观 徼 偶 感
    c# Mid 子窗体 盖住 控件
    pycharm 连接mysql时区问题
    django_drf
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13859981.html
Copyright © 2011-2022 走看看