Find Peak Element
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.
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的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 162. 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 --------- java
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- java+eclipse+selenium环境搭建
这几天在学selenium,大头虾的我.安装环境还是遇到了挺多问题,赶紧来记录下.不然下次又...(参考虫师的<Selenium2 Java自动化测试实战>),就随便写写加深下自己的印象. ...
- PHP Windows环境部署
1. 说明 本文用来在windows环境下手工搭建PHP开发环境,安装的功能模块主要包含MySQL,PHP以及Apache三个,环境如下: l Window7(64位) l MySQL 5.7.14 ...
- 我的CPG插件 (什么是CPG,就是跟号称全球唯一C++编写的魔镜是一样的格式的)
- excel小技巧
数据呈文本格式,怎么改成数字? 数据前有'号,如何去掉? 为什么数据格式在修改后需要再双击一下单元格才改过来了? 解决办法:你选中需要更改格式的那列 数据 ...
- Xcode工作区间xxxx.xcworkspace不包含xxxx.xcodeproj
一.问题描述 项目用到cocoapods管理第三方框架,所以需要打开xxxx.xcworkspace,Pods正常显示,但xxxx.xcodeproj显示红色,不包含xxxx.xcodeproj并且无 ...
- GitHub Desktop+码云(GIT.oschina)使用方法
一.如何从码云GIT导入到GitHubDeskTop桌面工具. 1.先用命令行切换到本地的目录. 2.使用git clone 码云GIT地址 命令将项目克隆到本地. 3.在GitHub Desktop ...
- File文件的使用
线程的停止: 1.停止一个线程一般是通过一个变量来控制 2.如果需要停止一个处于一个等待状态的线程,那么需要配合interrupture方法来完成 守护线程:(后台线程):在一个进程中只剩下守护线程, ...
- React学习笔记-4-什么是生命周期
什么是声明周期?组件本质上就是状态机,输入确定,输出一定确定.如何理解这一点?react有两个特点,第一个就是去除了所有的手动dom操作,也就是使用jsx.第二个就是组件把状态和结果一一对应起来,从而 ...
- JavaScript常用字符串操作方法总结
1.判断是否为字符串:typeof() var str = 'abcd'; typeof(str); //string 2.获取字符串的长度:length var str = '123456789 ...
- C语言-纸牌计算24点小游戏
C语言实现纸牌计算24点小游戏 利用系统时间设定随机种子生成4个随机数,并对4个数字之间的运算次序以及运算符号进行枚举,从而计算判断是否能得出24,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...