zoukankan      html  css  js  c++  java
  • 剑指offer

    旋转数组的最小数字

    问题描述:

    • 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
    • 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
    • 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为 1。
    • NOTE:给出的所有元素都大于 0,若数组大小为 0,请返回 0。

    方法一:二分查找法

    function minNumberInRotateArray(rotateArray) {
      // write code here
      if (!rotateArray.length) return 0;
      var left = 0;
      var right = rotateArray.length - 1;
      while (left + 1 < right) {
        var mid = Math.floor((left + right) / 2);
        if (rotateArray[mid] >= rotateArray[left]) {
          left = mid;
        } else {
          right = mid;
        }
      }
      return rotateArray[right];
    }
    

    方法二:扩展运算符

    function minNumberInRotateArray(rotateArray) {
      // write code here
      if (!rotateArray.length) {
        return 0;
      } else {
        return Math.min(...rotateArray);
      }
    }
    
  • 相关阅读:
    《PHP
    2018/06/11 数据库设计规范
    RequireJs 与 SeaJs的相同之处与区别
    null 与 undefinded
    page 分页
    fullPage的使用
    touch事件(寻找触摸点 e.changedTouches)
    t添加最佳视口
    随鼠标动的炫彩小球
    随机小球
  • 原文地址:https://www.cnblogs.com/muzidaitou/p/12713214.html
Copyright © 2011-2022 走看看