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 ...
随机推荐
- FreeBSD_11-系统管理——{Part_4 - 内核参数定制}
特别提醒:自行定制的内核,必須经过全方位测试无誤后,方能用于生产环境 基于:/usr/src/sys/amd64/conf/GENERIC cpu HAMMER ident TEST_kernel # ...
- ios php RSA 非对称加密解密 der 和pem生成
ios 使用public_key.der加密 php 使用 private_key.pem解密 openssl req -x509 -out public_key.der -outform der - ...
- android stuido build 慢的解决办法
Enable Offline Work: Click File -> Settings. Search for "gradle" and click in Offline w ...
- jquery checkbox选中、改变状态、change和click事件
jquery判断checked的三种方法:.attr('checked); //看版本1.6+返回:”checked”或”undefined” ;1.5-返回:true或false.prop('che ...
- redis-cli 命令总结
redis-cli 命令总结 Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用.在编程时,比如使用Redis 的Java语言包,这 ...
- Text input(文本输入框)
Text input(文本输入框)是用来获得用户输入的绝佳方式. 你可以用如下方法创建: <input type="text"> 注意,input元素是自关闭的.
- Activity的四种启动模式详解
Activity的启动模式在清单文件AndroidManifest.xml中的Activity属性中进行设置: 如:<activity android:name=".MainActiv ...
- Attribute "lazy" with value "true" must have a value from the list "false proxy no-proxy "
Hibernate 3.2 版本 当设置lazy="true"属性时,会产生该个异常: Attribute "lazy" with value "tr ...
- DM9000网卡驱动接受数据从中断方式改成NAPI方式小记
平台是最最经典的s3c2440了,说了要做这件事情很久了,就是改几行代码,一直没有做.前几天逼了自己一下,终于给做了,拖延症患者伤不起. 以下是需要读者对napi机制有所熟悉: step1:在boar ...
- CentOS7 安装 mongodb
https://docs.mongodb.com/manual/tutorial/install-mongodb-enterprise-on-red-hat/