LeetCode: Find Peak Element 解题报告
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.
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 解题报告的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 【原创】leetCodeOj --- Find Peak Element 解题报告
题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...
- LeetCode 169 Majority Element 解题报告
题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- 一种使用pyinstaller时图标问题解决方案
一种使用pyinstaller时图标问题解决方案 0x00 场景 使用pyinstaller将.py文件编译成.exe文件时,想要使用自己心仪的图标(.ico)比较麻烦.在使用pyinstalle ...
- 附003.Docker Compose命令详解
一 Docker Compose命令格式 Usage: docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...] docker- ...
- 由自定义事件到vue数据响应
前言 除了大家经常提到的自定义事件之外,浏览器本身也支持我们自定义事件,我们常说的自定义事件一般用于项目中的一些通知机制.最近正好看到了这部分,就一起看了下自定义事件不同的实现,以及vue数据响应的基 ...
- 关于Git的总结
首先我们先看一张图: 首先我们必须要先理解这几个概念:暂存区,本地仓库,远程仓库 暂存区:这个是我们每一次进行代码修改的地方,例如我们ieda的所编译的代码就是缓存区 本地仓库:是我们每一次pull, ...
- idea颜色主题
作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com IDEA 主题样式 === 这个垂直线的 颜 ...
- [CodeVS4633][Mz]树链剖分练习
思路: 轻重链剖分+线段树. #include<cstdio> #include<vector> #include<cstring> ; std::vector&l ...
- php get_magic_quotes_gpc()函数使用
magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post.get.cookie过来的数据增加转义字符"\",以确保这些数据不会引起程序,特别 ...
- The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly referenced from required .class files
我的方法:是缺少servlet的引用库,解决如下 1.工程右键-properties->java build path 2.在java build path的libraries tab页中选择A ...
- J-Link GDB Server Command
J-Link GDB Server - SEGGER Hilden, Germany – September 15th, 2011 – SEGGER Microcontroller today ann ...
- Voltage Translation for Analog to Digital Interface ADC
Voltage Translation for Analog to Digital Interface 孕龙逻辑分析仪 ZeroPlus Logic Analyzer How to modify an ...