zoukankan      html  css  js  c++  java
  • 剑指 Offer 11. 旋转数组的最小数字 Java

    可以想到,数组中会出现“断层”,直接遍历一次即可。不存在【1,2,3,4,5】旋转成【5,4,3,2,1】的情况。

    暴力法(我感觉还行啊,为什么被叫暴力):

    class Solution {
        public int minArray(int[] numbers) {
            int n = numbers.length;
            for(int i=0;i<n-1;i++){
                if(numbers[i+1] < numbers[i]){
                    return numbers[i+1];
                }
            }
            return numbers[0];
        }
    }
    

    二分查找法:(LeetCode上直接粘的):

    class Solution {
        public int minArray(int[] numbers) {
            int low = 0;
            int high = numbers.length - 1;
            while (low < high) {
                int pivot = low + (high - low) / 2;
                if (numbers[pivot] < numbers[high]) {
                    high = pivot;
                } else if (numbers[pivot] > numbers[high]) {
                    low = pivot + 1;
                } else {
                    high -= 1;
                }
            }
            return numbers[low];
        }
    }
    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/solution/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-by-leetcode-s/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
  • 相关阅读:
    控制论学习
    离开
    Python学习
    GHSpro多数据库连接
    django基础 第五章 Django连接数据库
    django基础 第四章 模板标签
    django基础 第三章 模板变量
    django基础 第二章 url配置及文件渲染
    django基础 第一章 环境搭建
    python基础 六、模块和包
  • 原文地址:https://www.cnblogs.com/yu-jiawei/p/13359076.html
Copyright © 2011-2022 走看看