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 ...
随机推荐
- 团队作业week8
每个团队编写一个团队项目的功能规格说明书.
- 2.Unable to instantiate Action, templateAction, defined for 'template_list' in namespace '/'templateAction
1.错误说没有命名空间'templateAction,但是在struts里写了这个,名字跟Action的名字是一样的,为什么会报这个错误 2.反复检查路径和名字,都没有问题 3.发现没有对其进行注入操 ...
- ubuntu 14.04加入五笔输入法
快捷键"ctrl+AIt+T",弹出终端,输入以下指令 //先卸载IBUS输入法 killall ibus-daemon sudo apt- get purge ibus ibus ...
- heartbeat安装与配置
Hearbeat和keepalived区别 Keepalived使用的vrrp协议方式,虚拟路由冗余协议 (Virtual Router Redundancy Protocol,简称VRRP): He ...
- The vboxdrv kernel module is not loaded
背景: 在没有关虚拟机的情况下, 直接关了电脑, 我的电脑系统是Centos 6 错误的提示: 在终端执行virtualbox -v 时提示 The vboxdrv kernel module is ...
- 对IEnumerable<T>和IQueryable<T>的一点见解
今天学习了用EF模型做查询,感觉数据库上下文对象的扩展方法很强大,所以研究了一下where的实现原理,其中遇到了一个问题,就是关于IEnumerable和IQueryable的区别,所以查了查资料,这 ...
- 六款值得推荐的android(安卓)开源框架简介
1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1) JSON,图像等的异步下载: (2) 网络请求的排序(scheduli ...
- 《Linux内核设计与实现》读书笔记(十四)- 块I/O层
最近太忙,居然过了2个月才更新第十四章.... 主要内容: 块设备简介 内核访问块设备的方法 内核I/O调度程序 1. 块设备简介 I/O设备主要有2类: 字符设备:只能顺序读写设备中的内容,比如 串 ...
- MongoDB 基础命令使用学习记录
1. 启动 mongod 几个常用命令说明:--dbpath : 指定数据库相关文件的存储目录 --logpath: 指定日志文件的存储目录 --port: 指定数据库的端口,默认是 27017 -- ...
- 淘宝TFS分布式文件系统内部实现
TFS文件名的结构 TFS的文件名由块号和文件号通过某种对应关系组成,最大长度为18字节.文件名固定以T开始,第二字节为该集群的编号(可以在配置项中指定,取值范围 1~9).余下的字节由Block I ...