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.

click to show spoilers.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

public class Solution {
public int findPeakElement(int[] num) {
return find(num,0,num.length-1);
}
int find(int[] A, int l, int r){
while(l<r){
int center=(l+r)/2;
if(center==l&¢er+1<=r&&A[center+1] <= A[center]||center==r-1&¢er-1>l&&A[center-1]<=A[center]||
center>l&¢er<r&&A[center-1] <= A[center]&&A[center+1] <= A[center]){
return center;
}else if(center>l&&A[center]<A[center-1]){
return find(A,l,center-1);
}else{
return find(A,center+1,r);
}
}
return l;
}
} 自己的个人博客:bingtel-木犹如此的博客, 有兴趣可以关注下

Find Peak Element的更多相关文章

  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. [LintCode] Find Peak Element 求数组的峰值

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

  4. lintcode 75 Find Peak Element

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

  5. 【leetcode】Find Peak Element

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

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

  7. LeetCode Find Peak Element

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

  8. 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 162. Find Peak Element --------- java

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

随机推荐

  1. 解决ssh localhost中root@localhost:要求输入密码问题(已经进行了无密码设置登录)

    首先删除~/.ssh目录下的3个文件,如下 id_rsa authorized_keys id_rsa.pub 然后 exit # 退出刚才的 ssh localhostcd ~/.ssh/ # 若没 ...

  2. 更改WAS Profiles的概要文件的server1的SDK版本

    WebSphere只能使用IBM JDK 哦,不能使用sun的JDK哦.不过如果只是改jdk的版本的话可以参考如下步骤:(以集群为例,假设具有管理节点Dmgr01,应用概要AppSrv01) 1. 确 ...

  3. java项目上线过程

    关于如何将Javaweb上线,部署到公网,让全世界的人都可以访问的问题.小编将作出系列化,完整的流程介绍. 1.在myeclipse中开发好项目,打包成war格式,不会的同学参考以下 http://z ...

  4. MongoDB 学习笔记一: 配置

    下载MongoDB 下载地址:https://www.mongodb.com/download-center?jmp=nav#community 这里是在windows平台下安装MongoDB, 下载 ...

  5. Lintcode 372. O(1)时间复杂度删除链表节点

    ----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...

  6. BigDecimal 处理集合

    1  创建一个BigDecimal 对象 BigDecimal Sum = new BigDecimal(0); 2  一个BigDecimal 对象,保留2位小数点 Sum.setScale(2,B ...

  7. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  8. python 学习第二天

    由于换了博客,第一篇没有在博客园写,写在了开源中国上,链接地址为http://my.oschina.net/u/254063/blog/719289,大家有兴趣可以看看 一, python 数据类型 ...

  9. linux中安装mysql数据库

    遇到ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' ...

  10. CSS预处器的对比——Sass、Less和Stylus

    预处器的对比--Sass.LESS和Stylus 转载: 英文原文:http://net.tutsplus.com/tutorials/html-css-techniques/sass-vs-less ...