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. Android 获得AndroidManifest文件里自定义的meta标签内容

    try { ApplicationInfo appInfo= this.getPackageManager().getApplicationInfo(getPackageName(),PackageM ...

  2. Android 自动安装脚本

    建立一个install.bat,写入下面 adb install -r %1PAUSE 把apk拖拽到install.bat上

  3. Feature Access

    在ArcGIS Server中发布支持Feature Access地图服务,你需要知道的几点: 所绘制的mxd地图文件中包含的数据,必须来自企业级数据库链接: mxd中包含的所有图层的数据,必须来自同 ...

  4. SQLite.Net-PCLUSING SQLITE IN WINDOWS 10 UNIVERSAL APPS

    USING SQLITE IN WINDOWS 10 UNIVERSAL APPS 1.下载SQLite VSIX package并安装 http://sqlite.org/download.html ...

  5. ShareSDK分享失败的原因

    关于分享估计很多都用的是ShareSDK的社会化分享,简单方便,支持的种类很多,但是一般的话都还是QQ,微信,新浪微博,腾讯微博为主. 最近需要导入一个分享的模块,失败了几次之后最终成功,分享给大家, ...

  6. sqlserver 中数据导入到mysql中的方法以及注意事项

    数据导入从sql server 到mysql (将数据以文本格式从sqlserver中导出,注意编码格式,再将文本文件导入mysql中): 1.若从slqserver中导出的表中不包含中文采用: bc ...

  7. MVC 解决 readonly 问题

    <input type="text" class="form-control" name="UR_UserName" value=&q ...

  8. vmware中虚拟机与主机ping不通,桥接模式,IP地址在同一网段,无法互ping!

    现象描述:网卡选用的桥接模式,IP地址在同一个网段,虚拟机内部可以正常上网,但是Guest OS和Host OS无法互ping! 原因:虚拟机里的防火墙没有关闭,导致禁用ping功能. 解决方法:关闭 ...

  9. 关闭电脑SSD的磁盘碎片整理

    小白往往会把机械硬盘时代的习惯带进固态硬盘时代,比如碎片整理.机械硬盘时代砖家最喜欢告诉小白:“系统慢了吧?赶紧碎片整理撒.”小白屁颠屁颠地整理去了.殊不知碎片整理对于SSD来说完全就是种折磨.这种“ ...

  10. Android-Gallery[使用C# And Java实现]

    运行效果 C#实现 using Android.App; using Android.OS; using Android.Widget; namespace ImageDemo { [Activity ...