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 ...
随机推荐
- 《Android进阶之光》--注解与依赖注入框架
No1: 标准注解: 1)@Override:覆写 2)@Deprecated:过时 3)@SuppressWarnings:取消警告 4)@SafeVarargs:申明使用了可变长度参数的方法 No ...
- 在macOS下正确配置 VS Code 使用 virtualenv 里的 python 环境参数
在macos配置好并启动 virtualenv 环境后,如何让 VS Code 使用这个环境下来编译调试 python 脚本呢? 1.首先当然是先配置好python虚拟环境 假定配置python的的虚 ...
- HDU - 1712 - ACboy needs your help 【分组背包】
<题目链接> 题目大意:有n个课程,现在花M天来学习这些课程,学习每个课程花的天数所得到的价值不同,求M天怎么分配学习才能得到的价值最大.(这些课程得到的价值和所花天数的关系由矩阵给出) ...
- SpringMVC页面传值
public ModelAndView query(){ ModelAndView modelAndView = new ModelAndView(); List list = new ArrayLi ...
- SpringMVC统一转换null值为空字符串的方法
在SpringMVC中,可以通过在<mvc:annotation-driven>中配置<mvc:message-converters>,把null值统一转换为空字符串,解决这个 ...
- ORB-SLAM2(一)----使用Eclipse进行开发
1.导入项目 准备工作 1, first we should make sure the compile with build.sh under ORB_SLAM2-master is OK. 2, ...
- android: shell 命令
adb是Android重要工具之一,以提供强大的特性,例如复制文件到设备或从设备复制文件.可以使用Android Shell命令行参数连接到手机本身,并发送基本的 shell 命令. 进入命令行,使用 ...
- 移动端web禁止长按选择文字以及弹出菜单
/*如果是禁用长按选择文字功能,用css*/ * { -webkit-touch-callout:none; -webkit-user-select:none; -khtml-user-select: ...
- caffe出错:Unknown bottom blob 'data' (layer 'conv1', bottom index 0)
原文https://blog.csdn.net/u011070171/article/details/75425740 caffe训练出现如下错误: Unknown bottom blob 'data ...
- iOS:针对固定数据源,更好的封装cell
一.介绍 在iOS开发中,tableView非常常用,能将其展示出来,它的数据源必不可少.当然数据源有动态下发的,有固定写死的,这里我只探讨固定写死的情况.对于死数据,我们在项目中经常遇到的场景就是我 ...