lintcode : find peak element 寻找峰值
题目
寻找峰值
你给出一个整数数组(size为n),其具有以下特点:
- 相邻位置的数字是不同的
- A[0] < A[1] 并且 A[n - 2] > A[n - 1]
假定P是峰值的位置则满足A[P] > A[P-1]
且A[P] > A[P+1]
,返回数组中任意一个峰值的位置。
给出数组[1, 2, 1, 3, 4, 5, 7, 6]
返回1
, 即数值 2 所在位置, 或者6
, 即数值 7 所在位置.
数组可能包含多个峰值,只需找到其中的任何一个即可
解题
直接遍历
Java
class Solution {
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
public int findPeak(int[] A) {
// write your code here
if(A == null || A.length == 0)
return -1;
if(A.length == 1)
return 0;
for(int i=1;i<A.length-1; i++){
if(A[i] >A[i-1] && A[i] >A[i+1])
return i;
}
if(A[0]>A[1])
return 0;
if(A[A.length-1] >A[A.length - 1])
return A.length - 1;
return -1;
} }
Python
class Solution:
#@param A: An integers list.
#@return: return any of peek positions.
def findPeak(self, A):
# write your code here
if A == None or len(A) ==0:
return -1
for i in range(1,len(A)-1):
if A[i]>A[i-1] and A[i]>A[i+1]:
return i
if A[0]>A[1]:
return 0
if A[len(A)-1] >A[len(A)-2]:
return len(A)-1
return -1
lintcode : find peak element 寻找峰值的更多相关文章
- 162 Find Peak Element 寻找峰值
峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...
- Leetcode162. Find Peak Element寻找峰值
示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2: 或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...
- [LintCode] Find Peak Element 求数组的峰值
There is an integer array which has the following features: The numbers in adjacent positions are di ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- Lintcode: Find Peak Element
There is an integer array which has the following features: * The numbers in adjacent positions are ...
- LintCode "Find Peak Element II"
Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...
- Leetcode之二分法专题-162. 寻找峰值(Find Peak Element)
Leetcode之二分法专题-162. 寻找峰值(Find Peak Element) 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1] ...
- LeetCode 162. 寻找峰值(Find Peak Element) 29
162. 寻找峰值 162. Find Peak Element 题目描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元 ...
- [Swift]LeetCode162. 寻找峰值 | Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
随机推荐
- jQuery 判断所有图片加载完成
对于图片的处理,例如幻灯片播放.缩放等,都是依赖于在所有图片完成之后再进行操作. 今天来看下如何判断所有的图片加载完成,而在加载完成之前可以使用 loading 的 gif 图表示正在加载中. 一.普 ...
- 如何开启MYSQL远程连接权限
开启MYSQL远程连接权限 //建议设置固定IP mysql> GRANT ALL PRIVILEGES ON *.* TO root@"8.8.8.8" IDENTIFIE ...
- 有关Mysql连接问题
问题一——Mysql number Error 2003 MySQL连接错误.错误代码10061.10061一般是Mysql服务没启动.或Mysql服务器无法连接 . 在程序栏找到Mysql\Mysq ...
- 给view 添加事件
//绑定图片点击事件 UITapGestureRecognizer *g=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@select ...
- Lucene 3.0
http://www.cnblogs.com/forfuture1978/archive/2010/02/22/1671487.html http://www.cnblogs.com/jiekzou/ ...
- 小课堂Week10 例外处理设计的逆袭Part3
小课堂Week10 例外处理设计的逆袭Part3 今天是<例外处理设计的逆袭>这本书阅读的第三天,也是最后一天,我们会主要通过实例,对Part2中提出的例外处理等级进行解读. Level1 ...
- Oracle体系结构知识点的运用
体系结构方面的优化问题: 设数据库很大,访问量非常高,共享池很小:这样共享池里面就无法存储很多解析过得sql语句,导致很多硬解析,这样数据库就非常缓慢.这个时候要加大共享池.如果是自动管理,就加大SG ...
- Andorid手机振动器(Vibrator)的使用
标签: android vibrator 震动器 it 分类: Andorid 获取振动器Vibrator实例: Vibrator mVibrator = (Vibrator) context.ge ...
- ios上取得设备唯一标志的解决方案
iOS 7中苹果再一次无情的封杀mac地址,现在已经不能获取ios7设备的物理地址.那么在开发中如何才能标识设备的唯一性呢?apple公司提供的方法是通过keychain来存一些标志信息,然后通过存的 ...
- hornetq 入门(1)
Hornetq 版本2.4.0final 需要JDK7及以上 Hornetq官网 Hornetq2.1中文手册 step1.启动服务端 1.1准备配置文件(配置说明参考官网手册) hornetq-c ...