852. Peak Index in a Mountain Array -- Easy

方法一:二分查找

int peakIndexInMountainArray(vector<int>& A) {

    // insert another two elements to avoid out of bound
const int INT_MAX_ = 2147483647;
const int INT_MIN_ = (-INT_MAX_-1);
// insert INT_MIN_ before A.begin()
A.insert(A.begin(),INT_MIN_);
A.push_back(INT_MIN_);
int left = 1, right = A.size()-2; // binary search
while(left <= right) {
int mid = left + (right - left) /2;
if(A[mid-1] < A[mid] && A[mid] > A[mid+1]) return mid-1;
if(A[mid-1] < A[mid] && A[mid] < A[mid+1]) left = mid + 1;
if(A[mid-1] > A[mid] && A[mid] > A[mid+1]) right = mid - 1;
} return -1;x }

官方答案:

class Solution {
public int peakIndexInMountainArray(int[] A) {
int lo = 0, hi = A.length - 1;
while (lo < hi) {
int mi = lo + (hi - lo) / 2;
if (A[mi] < A[mi + 1])
lo = mi + 1;
else
hi = mi;
}
return lo;
}
}
  • Time Complexity: O(logN)
  • Space Complexity: O(1)

方法二:遍历找到数值下降的点

略。

参考:

LeetCode 852. Peak Index in a Mountain Array C++ 解题报告的更多相关文章

  1. LeetCode 852. Peak Index in a Mountain Array (山脉数组的峰顶索引)

    题目标签:Binary Search 题目给了我们一组 int array,让我们找到数组的 peak. 利用 binary search, 如果数字比它后面那个数字小,说明还在上坡,缩小范围到右半边 ...

  2. LeetCode 852 Peak Index in a Mountain Array 解题报告

    题目要求 Let's call an array A a mountain if the following properties hold: A.length >= 3 There exist ...

  3. LeetCode 852. Peak Index in a Mountain Array(C++)

    Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...

  4. leetcode 852. Peak Index in a Mountain Array

    Input: [0,1,0] Output: 1 Input: [0,2,1,0] Output: 1解: 比较数组中的i和i-1的大小,如果前一位大于后一位数字,前一位则是结果 let ans = ...

  5. 【Leetcode_easy】852. Peak Index in a Mountain Array

    problem 852. Peak Index in a Mountain Array solution1: class Solution { public: int peakIndexInMount ...

  6. 【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...

  7. 852. Peak Index in a Mountain Array

    class Solution { public: int peakIndexInMountainArray(vector<int>& A) { return max_element ...

  8. Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array)

    Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array) 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 ...

  9. [LeetCode] Peak Index in a Mountain Array 山形数组的顶峰坐标

    Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...

随机推荐

  1. JS-3

    运算符 数学运算符 + - * / %(取模运算符) js内置一个对象叫Math,Math提供了很多关于计算的方法,常见的 // 随机数 console.log(Math.random()); // ...

  2. java第六次课后作业

    class Check{ public boolean validate(String name, String password){ if(name.equals("shenhaochen ...

  3. Java作业:第一次过程性考核 ——长春职业技术学院 16级网络工程

    Java作业:第一次过程性考核 ••<结构化程序设计>•• 考核目标:初步了解Java基本语法规则,学习结构化程序设计思想. 码云链接:https://gitee.com/SoridoD/ ...

  4. python入门(五)

    一.函数返回值 1.函数如果返回多个值,他会把这几个值放到一个元组里面 2.也可以用多个变量来接收 返回多个值放到元组里面 def say(): num1=1 num2=2 num3=3 return ...

  5. POJO、JavaBean、DTO的区别

    一.POJO(Plain Ordinary Java Object)简单的Java对象,其中有一些属性及其getter setter方法的类,没有业务逻辑(重点理解一下"没有业务逻辑&quo ...

  6. 关注Yumiot公众号,了解最新的物联网资讯

    Yumiot,专注于物联网行业,每天不定期推送最新的物联网行业新闻.详情请用微信搜索关注  yumiot  .

  7. 拷贝一张图片,从一个目录到另外一个目录下(PS:是拷贝是不是移动)

    package com.lanxi.demo2_6; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...

  8. css之positon与z-index

    在网页设计中,position属性的使用是非常重要的.有时如果不能认识清楚这个属性,将会给我们带来很多意想不到的困难. position属性共有四种不同的定位方法,分别是static.fixed.re ...

  9. windows中obs源码编译的坑

    好用的版本: cmake-3.6.1-win64-x64  +  vs2015  + qt-opensource-windows-x86-msvc2015_64-5.7.0   +   obs-stu ...

  10. jmeter之关联

    前言:当请求之间有依赖关系,比如一个请求的入参是另一个请求返回的数据,这时候就需要用到关联处理,Jmeter可以通过“后置处理器”中的“正则表达式提取器”来处理关联. 一.后置处理器-------正则 ...