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 ...
随机推荐
- 陈国凯oi历程
从此成了OI退役狗 说实话,当时NOIP比赛前就想写这篇,结果一直没有足够的动力和时间写,现在高考完了,也有了时间,就写一点东西,记录一下我的OI经历吧. 初入OI 高一时,我是信息技术课代表(当然没 ...
- HBase 开发环境搭建(Eclipse\MyEclipse + Maven)
写在前面的话 首先, 搭建基于MyEclipse的Hadoop开发环境 相信,能看此博客的朋友,想必是有一定基础的了.我前期写了大量的基础性博文.可以去补下基础. 比如, CentOS图形界面下如何安 ...
- IdentityServer4-端点
一.发现端点 二.授权端点 三.令牌端点 四.UserInfo端点 五.Introspection端点 六.撤销端点 七.结束会话端点 一.发现端点 发现端点可用于检索有关IdentityServer ...
- 50 tips of JavaScript,这些坑你都知道吗?
1.在局部作用域中,使用var操作符定义的变量将成为定义该变量的作用域中的局部变量,省略var的会创建全局变量:在全局作用域中,不管是否使用var操作符定义的变量都会创建一个全局变量.但是,在全局作用 ...
- Web前端性能优化进阶——完结篇
前言 在之前的文章 如何优化网站性能,提高页面加载速度 中,我们简单介绍了网站性能优化的重要性以及几种网站性能优化的方法(没有看过的可以狂戳 链接 移步过去看一下),那么今天我们深入讨论如何进一步优化 ...
- 10.23 正睿停课训练 Day7
目录 2018.10.23 正睿停课训练 Day7 A 矩形(组合) B 翻转(思路) C 求和(思路 三元环计数) 考试代码 B1 B2 C 2018.10.23 正睿停课训练 Day7 期望得分: ...
- maven遇到的一些问题
(1)在加载的时候pom.xml文件是和src同级的,当在pom.xml导入jar包时一定要先把jar包所在的项目mvn install否则会报找不到的错误 (2)怎么找到maven原有的仓库? 找到 ...
- window7 更改电脑黑屏时间
无废话--------------------Window7 更改电脑黑屏时间,步骤如下: 1.进入‘控制面板’,通过开始页面或通过计算机我的电脑中的打开‘控制面板’都可以打开. 2.系统与安全类别下 ...
- 用c#监控网络状态
1.查询当前网络状态: using Microsoft.VisualBasic.Devices; //判断当前网络连接状态 Network nw=new Network(); if(nw.IsAvai ...
- C#并行Parallel编程模型实战技巧手册
一.课程介绍 本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的一部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享出来给大家进行学习,不断的收集.整理和 ...