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的更多相关文章

  1. [LeetCode] Find Peak Element 求数组的局部峰值

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  2. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  3. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  4. [LintCode] Find Peak Element 求数组的峰值

    There is an integer array which has the following features: The numbers in adjacent positions are di ...

  5. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...

  6. 【leetcode】Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  7. ChIP-seq Peak caller MACS index out of range问题解决

    使用MACS1.4 进行peak calling的时候发现一个比较奇怪的问题: 我的某些文件无法被MACS1.4 进行peak calling,出现如下的信息: Traceback (most rec ...

  8. 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] ≠ ...

  9. LeetCode Find Peak Element

    原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...

随机推荐

  1. Java—继承、封装、抽象、多态

    类.对象和包 1) 面向对象编程(Object Oriented Programming ,简称 OOP):20世纪70年代以后开始流行. 2) 结构化编程与面向对象编程的区别: A. 在结构化编程中 ...

  2. 关于e^PI>PI^e

  3. GitLab的Gravatar头像服务不可用

    由于www.gravatar.com在国内不可正常使用,导致我们搭建的GitLab在网页上会阻塞大量时间,并最终无法显示头像.我们可以将其替换成"多说"的头像服务.我使用的是CE ...

  4. 无需部署的轻量级数据库—SQLLite,使用Demo

    当有程序需要保存轻量数据,而又烦躁序列化到本地的不便,轻量级数据库—SQLLite是一个很好的选择,只需引用System.Data.SQLite.DLL,无需部署数据库,便可像拥有数据库一样保存数据, ...

  5. Capistrano初探--Ruby快速部署工具

    1.Capistrano介绍 是什么?---一种部署工具.(部署就是在生产服务器上安装应用程序,或是更新最新版本:web服务器的启动重启与停止:使网站进入维护状态或将其恢复为常态) 在进行 Rails ...

  6. 修改VNC分辨率大小

    实验系统是centos6.5,在被连接的机器上需要安装vncserver. 1.第一种方法:使用geometry参数进行调整使用man命令获得关于geometry参数的描述[root@secdb ~] ...

  7. poj 1556 The Doors

    The Doors Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u   Java ...

  8. What's New in C# 6.0

    Static Types as using So, we are all quite familiar with this notion of accessing static class membe ...

  9. 【ASP.NET Web API教程】6.4 模型验证

    本文是Web API系列教程的第6.4小节 6.4 Model Validation 6.4 模型验证 摘自:http://www.asp.net/web-api/overview/formats-a ...

  10. 高手速成android开源项目【导航篇】

    Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项目,贡献出他们的智慧和创造力.学习开源代码是掌握技术的一个 ...