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] ≠ ...
随机推荐
- 在input中放对象
var input = $("<input type='hidden' class='hidden-user'/>"); $(input).data("ran ...
- JTextField 限制指定字符不能输入
txtStartDate.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { int keyChar = e.ge ...
- shell 输出九九乘法表
#/bin/bash i= j= )) do while(($j<=$i)) do echo -ne $j×$i=$[$j*$i]"\t" j=$[$j+] done ech ...
- 用flex做垂直居中
<div class="flex-cont flex-centerbox"> <div class="center-cont"> < ...
- Mysql InnoDB 共享表空间和独立表空间
前言:学习mysql的时候总是习惯性的和oracle数据库进行比较.在学习mysql InnoDB的存储结构的时候也免不了跟oracle进行比较.Oracle的数据存储有表空间.段.区.块.数据文件: ...
- java.io.EOFException
错误代码如下: 严重: IOException while loading persisted sessions: java.io.EOFException java.io.EOFException ...
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...
- 用python实现一个不排序的列表功能
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- RSA密钥之C#格式与Java格式转换
前言 最近由于项目需求,服务端由c#编写,客户端由java编写.通信数据使用RSA非对称加密.但是java和c#生成的密钥格式是不一样的,所以需要转换格式才可以正常使用.网上搜到使用java进行格式转 ...
- SSH框架整合(全注解)
全部jar包 目录结构 配置案例 package cn.yzu.Tbook.action; import javax.annotation.Resource; import org.apach ...