题目:

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.

思路:

按照题意,num[0]是大于左边的不存在的那个元素的,num[size-1]也是大于右边那个不存在的元素的,假如不存在,那么就会有num[0]<num[1],num[1]<num[2],就是增序,num[size-2]<num[size-1],这样num[size-1]就是peak elem了,所以一定存在。于是就是这样的思路,num[NULL] < num[0],我们假设左边的元素小于右边的元素,那么第一个左边元素大于右边的那个一定是peak elem.如num[0]。

/**
* @param {number[]} nums
* @return {number}
*/
var findPeakElement = function(nums) {
var n=nums.length;
for(var i=1;i<n;i++){
if(nums[i]<nums[i-1]){
return i-1;
}
} return n-1;
};

【数组】Find Peak Element的更多相关文章

  1. [LeetCode] Find Peak Element 求数组的局部峰值

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

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

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

  3. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  4. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...

  5. LeetCode OJ 162. Find Peak Element

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

  6. Leetcode_162_Find Peak Element

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43415313 A peak element is an e ...

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

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

  8. Find Peak Element(ARRAY - Devide-and-Conquer)

    QUESTION A peak element is an element that is greater than its neighbors. Given an input array where ...

  9. 【Leetcode】【Medium】Find Peak Element

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

随机推荐

  1. ArcGIS Desktop python Add-in 测试一个插件

    a)制作一个插件文件 先找到工作目录,双击运行makeaddin.py脚本.这个脚本拷贝所有插件需要的文件和文件夹并在工作目录形成一个压缩文件.该压缩文件名为工作目录名称加上".esriad ...

  2. 极小极大搜索方法、负值最大算法和Alpha-Beta搜索方法

    1. 极小极大搜索方法    一般应用在博弈搜索中,比如:围棋,五子棋,象棋等.结果有三种可能:胜利.失败和平局.暴力搜索,如果想通过暴力搜索,把最终的结果得到的话,搜索树的深度太大了,机器不能满足, ...

  3. (动态规划 最长有序子序列)Monkey and Banana --HDU --1069

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1069 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. Codeforces805D. Minimum number of steps 2017-05-05 08:46 240人阅读 评论(0) 收藏

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  5. C++ 中数组做参数的分析

    C++ 中数组做参数的分析 1.数组降价问题? "数组引用"以避免"数组降阶",数组降阶是个讨厌的事,这在C语言中是个无法解决的问题,先看一段代码,了解什么是& ...

  6. shell 脚本 抽取指定数量的随机学生

    #!/bin/bash # #!/bin/bash # read -p '输入数' c #指定抽取的学生人数 jw=('王浩' '谢云生' '黄科杨' '何星宇' '张宸兵' '邓培林' '刘桃' ' ...

  7. delphi存取图片

    1.存图片到数据库 var   PicStream: TMemoryStream; if imgBugPic.Picture.Graphic <> nil then  begin    P ...

  8. Android-Java-Thread start run的区别

    Thread start(Thread子类.start(); 这样属于开启新的线程,不属于方法调用) Thread.currentThread().getName(); 获取当前正在运行的线程执行路径 ...

  9. Java类与类之间的继承关系

    Java父类与子类继承关系,调用的各种关系 示例一(子类调用父类函数): // 定义一类 A public class A { // 此方法打印一句话 public void a() { System ...

  10. ASP.NET MVC 富文本Ueditor编辑 后台传值前端乱码解决方案

    只是将当前内容String当成Html插入,我想是跟数据类型转换差不多 //把内容赋值给ueditor var ue = UE.getEditor('editor');//实例化 ue.ready(f ...