题目

寻找峰值

你给出一个整数数组(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. [Android教程]TextView使用SpannableString设置复合文本

    TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来对指定文本进行相关处理,具体有以下功能: 1.Bac ...

  2. 亚马逊左侧导航(jquery.menuaim.js)

    jquery.menuaim.js     主菜单 <div class="active"> <ul class="dropdown-menu" ...

  3. VS2010调试入门指南

    1 导言 在软件开发周期中,测试和修正缺陷(defect,defect与bug的区别:Bug是缺陷的一种表现形式,而一个缺陷是可以引起多种Bug的)的时间远多于写代码的时间.通常,debug是指发现缺 ...

  4. ccache高速编译工具

    ccache的主页:http://ccache.samba.org distcc的主页:http://distcc.samba.org 1.背景: 在处理一些规模相对较大的工程时,编译花费的时间可能会 ...

  5. 信息传递--NOIP2015 day1 T2--暴力

    这道题我用了判联通量加暴力,但联通量判炸了....然后从code[VS]上看到个不错的代码,就拿来了^_^... 基本思路是去掉环外的点,然后走每一个联通块. #include <iostrea ...

  6. [Android Training视频系列] 8.1 Controlling Your App’s Volume and Playback

    主要内容:1 鉴别使用的是哪个音频流2 使用物理音量键控制应用程序的音量 3 使用物理播放控制键来控制应用程序的音频播放 视频讲解:http://www.eyeandroid.com/thread-1 ...

  7. Version of SQLite used in Android?

    sing the emulators (adb shell sqlite3 --version): SQLite 3.7.11: 19-4.4-KitKat 18-4.3-Jelly Bean 17- ...

  8. iOS-音频格式转换-b

    iOS处理音频过程中有时候需要不同格式的音频进行转换,最近需要将m4a格式的音频转换成wav,在网上搜索之后代码整理如下: - (void)convetM4aToWav:(NSURL *)origin ...

  9. 复习JS和jQuery

    复习JS和jQuery 近些时日,以前学过的东西忘了好多.今天且写一点复习一下JS和jQuery.希冀与某年某月某日,忘却的时候,能看一下自己写的博文,尚可记起一二. 现在有需求如下:有两个按钮,一个 ...

  10. win8安装新字体

    http://jingyan.baidu.com/article/e3c78d640a7ab33c4c85f52d.html