题目

寻找峰值

你给出一个整数数组(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 寻找峰值的更多相关文章

  1. 162 Find Peak Element 寻找峰值

    峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...

  2. Leetcode162. Find Peak Element寻找峰值

    示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2:   或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...

  3. [LintCode] Find Peak Element 求数组的峰值

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

  4. [LeetCode] 162. Find Peak Element 查找峰值元素

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

  5. Lintcode: Find Peak Element

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

  6. LintCode "Find Peak Element II"

    Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...

  7. Leetcode之二分法专题-162. 寻找峰值(Find Peak Element)

    Leetcode之二分法专题-162. 寻找峰值(Find Peak Element) 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1] ...

  8. LeetCode 162. 寻找峰值(Find Peak Element) 29

    162. 寻找峰值 162. Find Peak Element 题目描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元 ...

  9. [Swift]LeetCode162. 寻找峰值 | Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

随机推荐

  1. CentOS设置服务开机启动的方法

    CentOS设置服务开机启动的两种方法 1.利用 chkconfig 来配置启动级别在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后 ...

  2. php header函数实例代码

    一个完美的演示PHP header()函数用法的完整代码. 其中介绍的refresh方法,比<META ……用起来更得心应手,应该是段不错的代码. <?php  /*** Function ...

  3. Android Material Design:ViewPager与android.support.design.widget.TabLayout双向交互联动切换

    通常,android.support.design.widget.TabLayout与Android的ViewPager联合使用,实现与ViewPager的切换与联动.(1)比如,当用户手指触摸选择T ...

  4. How to achieve dialog with lookup control

    How to create a dialog with the lookup as a control, the other control SalesId ItemId lookup is the ...

  5. [转]init.d解析

    本文为转载,放在这里以备忘. init.d指的是/etc/rc.d/init.d目录.本文包括3部分内容1. Linux的引导过程2. 运行级别3. /etc/rc.d/ 与/etc/rc.d/ini ...

  6. 通过数据绑定模板得到对应的Item控件

    这类控件都继承于Selector,其中主要有ComboBox.listview.listbox.datagrid. 由于个人对WPF的了解所有可能有遗漏,希望各位能够指出一起进步. 在遍历上面控件时主 ...

  7. iOS内存管理retain,assign,copy,strong,weak

    转自:http://www.cnblogs.com/nonato/archive/2013/11/28/3447162.html iOS的对象都继承于NSObject, 该对象有一个方法:retain ...

  8. VirtualBox虚拟机安装MSDOS和MINIX2.0.0双系统

    1. 在VirtualBox中新建一个MSDOS虚拟机. 2.下载一个MSDOS软盘镜像. 3.启动虚拟机,提示选择安装盘时,选择步骤2下载过来的MSDOS镜像. 4.正常启动进入DOS命令行,用FD ...

  9. AnyChartStock去除水印方法

    最近在使用AnyChartStock的图表,功能很强大,但下载过来是有水印的,虽然网上也有很多破解无水印的版本,但基本都是AnyChart的,AnyChartStoc的几乎没有.所以自己尝试着去除水印 ...

  10. php protected只能被继承,不可以在实例中调用,parent::调用父类(子类函数的重载对父类的函数没有影响)

    <?php class a { private function fun1(){ echo 'a1'; } //protected 可以被继承,但是只能在子类中使用,不能被实例化调用 prote ...