find the peak value
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.
计算一个数组的峰值,最简单的方法就是扫描数组,判断如果 中间值 > 左边值 && 中间值 > 右边值 那么我们就可以获取到该索引数值。算法的时间复杂度为O(n),代码如下所示:
public class Solution {
public int findPeakElement(int[] num) {
if(num.length== 1 ){
return 0;
}else if(num.length ==2){
return (num[0] > num[1]? 0:1);
}else{
for(int i = 1;i<num.length-1;i++){
if(num[i] > num[i-1] && num[i] > num[i+1]){
return i;
}
}
return (num[num.length-1] > num[0]? (num.length-1):0);
} }
}
我们尝试用二分法来解决这个问题,三个数无外乎四种情况,升序排列、降序排列、凸排列,凹排列。
public class Solution {
public int findPeakElement(int[] num) { return binarySearckPeakElement(0,num.length-1,num);
} public int binarySearckPeakElement(int left,int right,int[]num){
int mid =(right + left)/2;
int value = num[mid];
if(right - left == 0){
return left;
}
if(right - left ==1){
return (num[left] > num[right]? left:right);
}
if(value > num[mid-1] && value > num[mid+1] ){
return mid;
}else if(num[mid+1] > num[mid-1]){//升序排列
return binarySearckPeakElement(mid,right,num);
}else if(num[mid+1] < num[mid-1]){//降序排列
return binarySearckPeakElement(left,mid,num);
}else{
return binarySearckPeakElement(left,mid,num);
//binarySearckPeakElement(mid,right,num);
}
} public static void main(){
Solution obj = new Solution();
int []num = {12,324,54,13,43,2111,1};
int index = obj.findPeakElement(num);
System.out.println(index);
} }
时间复杂度为O(nlogn)
find the peak value的更多相关文章
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- [LintCode] Find Peak Element 求数组的峰值
There is an integer array which has the following features: The numbers in adjacent positions are di ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
- 【leetcode】Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- ChIP-seq Peak caller MACS index out of range问题解决
使用MACS1.4 进行peak calling的时候发现一个比较奇怪的问题: 我的某些文件无法被MACS1.4 进行peak calling,出现如下的信息: Traceback (most rec ...
- Java for LeetCode 162 Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode Find Peak Element
原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...
随机推荐
- 【Mysql】权限管理
mysql权限介绍 mysql中存在4个控制权限的表,分别为user表,db表,tables_priv表,columns_priv表. mysql权限表的验证过程为: 1.先从user表中的Host, ...
- 【Map】MapTest
package cn.itcast.p1.map.test; import java.util.HashMap; import java.util.Map; public class MapTest2 ...
- 用JS制作简易的可切换的年历,类似于选项卡
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 30.0px Consolas; color: #2b7ec3 } p.p2 { margin: 0.0px ...
- Excel VBA自动添加证书(二)
继续上次没有写完的随笔,本来是很想一次性写完的,但是到中午一点了还没有吃东西,其实饿的不行了,还好写博客时会自动保存,中间电脑实然蓝屏,花了二个多小时写的没有点击保存,吓我一下,以为会全没了. 前面讲 ...
- 弹出JS提示框
弹出JS提示框Page.ClientScript.RegisterStartupScript(typeof(string), "msg", "<script> ...
- Linux sort 多字段分组排序
常用参数: -t: 指定分隔符 -k: 指定域 -u: 去除重复行 -n: 以数值排序 -r: 降序排序 (sort默认的排序方式是升序) -o: 结果重定向输出到文件 1.源文件: # cat hh ...
- RecyclerView+CardView简单使用
RecyclerView取代Listview用来显示数据,除此之外还能实现瀑布流的布局.CardView让我们的界面更好看,此外还将使用官方的下拉刷新. 添加支持: compile 'com.andr ...
- Request Tracker 4.0.13 发布
Request Tracker 4.0.13 修复了几个重要的安全问题. Request Tracker,企业级的问题跟踪系统
- django rest_framework--入门教程
题设.如果官网DEMO能够正常跑起来请继续,如果不能请参考上一篇 1.新建MODEL 在数据库里添加相应的数据,可以使用命令 manage.py syncdb 这时候会建立对应的表 2.新建序列化方法 ...
- github心得
心得 : 1:安装:省略 2. 配置 Git 以及上传代码 安装 Git 成功后,如果是 Windows 下,选择 Git Bash ,在命令行中完成一切,可能开始有点麻 烦,不过就那几条命令行,用 ...