Find Peak Element

A peak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

click to show spoilers.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

SOLUTION 1:

线性查找,时间O(N):

 public int findPeakElement1(int[] num) {
if (num == null) {
return 0;
} if (num.length == 1) {
return 0;
} for (int i = 0; i < num.length; i++) {
if (i == 0) {
if (num[i] > num[i + 1]) {
return i;
}
continue;
} if (i == num.length - 1) {
if (num[i] > num[i - 1]) {
return i;
}
continue;
} if (num[i] > num[i + 1] && num[i] > num[i - 1]) {
return i;
}
} return -1;
}

SOLUTION 2:

使用九章算法的二分法模板,可以达到O(logN)的时间复杂度。原理是:

当找到一个下坡,我们往左移动,当找到一个上坡,我们往右移动,这样我们就可以达到顶峰。

如果找到一个山谷,则向任意方向移动即可。

4

3          3     5

2    2    2

1          1

如上图所示,3,4都是可能的解。

最后循环break时,把l,r的值找一个大的即可。

 public int findPeakElement(int[] num) {
if (num == null) {
return 0;
} if (num.length == 1) {
return 0;
} int l = 0;
int r = num.length - 1; while (l < r - 1) {
int mid = l + (r - l) / 2;
if (num[mid] > num[mid + 1] && num[mid] > num[mid - 1]) {
return mid;
} if (num[mid] > num[mid - 1] && num[mid] < num[mid + 1]) {
// rising area. move right;
l = mid;
} else if (num[mid] < num[mid - 1] && num[mid] > num[mid + 1]) {
r = mid;
} else {
l = mid;
}
} return num[l] > num[r] ? l: r;
}

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/binarySearch/FindPeakElement.java

LeetCode: Find Peak Element 解题报告的更多相关文章

  1. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  2. 【原创】leetCodeOj --- Find Peak Element 解题报告

    题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...

  3. LeetCode 169 Majority Element 解题报告

    题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. 《Gradle权威指南》--Android Gradle多项目构建

    No1: Android多项目设置 目录结构: MyProject/ setting.gradle app/ build.gradle libraries/ lib1/ build.gradle li ...

  2. macbook安装并破解Clion2018(Pycharm也一样)

    一.下载 Clion 并安装 二.修改hosts文件,使用 vim /private/etc/hosts 命令将 0.0.0.0 account.jetbrains.com 粘贴进去(提示: i键是插 ...

  3. Angular 个人深究(三)【由Input&Output引起的】

    Angular 个人深究(三)[由Input&Output引起的] 注:最近项目在做别的事情,angular学习停滞了 1.Angular 中 @Input与@Output的使用 //test ...

  4. SQLite中的SELECT子句使用别名

    SQLite中的SELECT子句使用别名 开发者可以使用AS关键字为指定的列名提供一个新的别名,其语法形式如下 SELECT column_name AS Alias [,…] 例如,下面的SQL语句 ...

  5. IE8 margin:0 auto 不能居中显示的问题

    ie8下面margin:0 auto;不能居中的解决方案,ie8兼容性代码 今天写了个div,用margin:0 auto:来定义他的属性,让他居中,结果,竟然无效. 一开始以为是css里的代码冲突了 ...

  6. 洛谷.1919.[模板]A*B Problem升级版(FFT)

    题目链接:洛谷.BZOJ2179 //将乘数拆成 a0*10^n + a1*10^(n-1) + ... + a_n-1的形式 //可以发现多项式乘法就模拟了竖式乘法 所以用FFT即可 注意处理进位 ...

  7. 2016年3月17日Android学习笔记

    1.Java.io.ByteArrayOutputStream.writeTo()方法实例 java.io.ByteArrayOutputStream.writeTo(OutputStream out ...

  8. Problem D: 指针函数

    Description YHZ自认为很聪明的人, 在C语言课上老师布置了一个作业,让能求正方形和圆的面积, 正当YHZ要跃跃欲试的时候, 老师却要求使用函数指针来实现这个功能,YHZ立马就不会了,他现 ...

  9. Matplotlib新手上路(上)

    matplotlib是python里用于绘图的专用包,功能十分强大.下面介绍一些最基本的用法: 一.最基本的划线 先来一个简单的示例,代码如下,已经加了注释: import matplotlib.py ...

  10. c++对象工厂

      一.简单工厂 #pragma once struct IObjectA { virtual void Test1()=0; }; class ObjectA:public IObjectA { p ...