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. Document.write和 InnerHTML

    Document.write 定义: Document.write()这种方法是将文本字符串写入document.open()打开的文档流. document.write()方法可以用在两个方面:页面 ...

  2. Python_Mix*random模块,time模块,sys模块,os模块

    random模块 作用: 生成随机数(整数,小数,从列表中随机抽值,打乱列表顺序) 常用函数: random.random( )生成随机小数 random.uniform( )取一个范围之间的小数 r ...

  3. Android 偏门xml属性

    在 recycleView listview scroview 等等 活动的时候会出现蓝边 android:overScrollMode="never" 用次属性可以去掉 fadi ...

  4. 一些不常用的Oracle用法记录(含模糊查询)

    (一).判断 某字段 是否包含 某字符串 select * from mc_member m where instr(m.bu, 'BU') > 0 --查询bu包含'BU' (二).将 某字段 ...

  5. 获取百度地图POI数据二(准备搜索关键词)

    上篇讲到  想要获取尽可能多的POI数据 需要准备尽可能多的搜索关键字   那么这些关键字如何得来呢?   本人使用的方法是通过一些网站来获取这些关键词   http://poi.mapbar.com ...

  6. java富文本编辑器KindEditor

    在页面写一个编辑框: <textarea name="content" class="form-control" id="content&quo ...

  7. 关于MySQL什么时候使用索引问题以及什么情况下应不建或少建索引

    一,什么情况下使用索引1. 表的主关键字 自动建立唯一索引 2. 表的字段唯一约束 ORACLE利用索引来保证数据的完整性 3. 直接条件查询的字段 在SQL中用于条件约束的字段 如zl_yhjbqk ...

  8. tensorFlow入门实践(二)模块化

    实现过一个例子之后,对TensorFlow运行机制有了初步的了解,但脑海中还没有一个如何实现神经网络的一个架构模型.下面我们来探讨如何模块化搭建神经网络,完成数据训练和预测. 首先我们将整体架构分为两 ...

  9. 简单透析cookies,sessionStorage和localStorage

    首先大致说一下 1.sessionStorage是会话层的一种存储方式,当会话关闭或者退出,sessionStorage就会被清除,有效期较短 2.localStorage是浏览器提供的本地存储方式, ...

  10. [RESTful] RESTful是什么,为什么要使用它

    RESTful是什么? 本质:一种软件架构风格 核心:面向资源 解决的问题:降低开发的复杂性,提高系统的可伸缩性 设计概念和准则: 1.网络上所有的事物都可以被抽象为资源 2.每个资源都有唯一的资源标 ...