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 if语句练习

    第一题:求一元二次方程的根 public class Lianxi1 { public static void main(String[] args) { System.out.println(&qu ...

  2. jsp_注释

    jsp支持两种注释的语法操作,一种是显示注释(在客户端允许看的见),另一种是隐式注释 显示注释:<!--注释内容--> 隐式注释: 格式一://单行注释 格式二:/*多行注释*/ 格式三: ...

  3. (二分)Block Towers(cf626)

    http://www.codeforces.com/contest/626/problem/C 题意是有一群小朋友在堆房子,现在有n个小孩每次可以放两个积木,m个小孩,每次可以放3个积木,最后每个小孩 ...

  4. alert()、confirm()和prompt()的区别

    1.警告消息框alertalert 方法有一个参数,即希望对用户显示的文本字符串.该字符串不是 HTML 格式.该消息框提供了一个“确定”按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是说,用 ...

  5. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. Digit Root ---- 余九定理

    题目:hdu1013, hdu1163, 51nod1116. or dr(n) = (n-1)%9+1. 其中,n-1是为了将结果0-8匹配到1-9. Reference: [1] https:// ...

  7. 不能运行,:framework not found SenTestingKit

    1. 真机调试,提示 ld: framework not found SenTestingKit $(DEVELOPER_LIBRARY_DIR)/Frameworks  

  8. OpenGL(一)——入门学习

    概要 1. 为什么使用OpenGL 2. 在VS2008上搭建环境 3. 一个简单的例程 OpenGL相较于DirectX的优越性 1. 与C语言紧密结合 OpenGL命令最初就是用C语言函数来进行描 ...

  9. hdu - 3959 Board Game Dice(数学)

    这道题比赛中没做出来,赛后搞了好久才出来的,严重暴露的我薄弱的数学功底, 这道题要推公式的,,,有类似于1*a+2*a^2+3*a^3+...+n*a^n的数列求和. 最后画了一张纸才把最后的结果推出 ...

  10. 十六进制转十进制函数_C编程

    /**************************十六进制转十进制函数**************************//*函数原型:uint htd(uint a)/*函数功能:十六进制转十 ...