【数组】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.
思路:
按照题意,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的更多相关文章
- [LeetCode] 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 di ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
- 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] ≠ ...
- Leetcode_162_Find Peak Element
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43415313 A peak element is an e ...
- [Swift]LeetCode162. 寻找峰值 | Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- 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 ...
- 【Leetcode】【Medium】Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- IP之ALTDDIO_in仿真
需要添加altera_mf库,才可以仿真. 上升沿输出,把前一个时钟的数据输出来. `timescale 1 ns/ 1 ns; module altddio_in_ip_tb; reg rst; r ...
- Java多线程-并发协作(生产者消费者模型)
对于多线程程序来说,不管任何编程语言,生产者和消费者模型都是最经典的.就像学习每一门编程语言一样,Hello World!都是最经典的例子. 实际上,准确说应该是“生产者-消费者-仓储”模型,离开了仓 ...
- (转) MVC身份验证及权限管理-1
转自:http://blog.csdn.net/kenshincui/article/details/5559508 MVC自带的ActionFilter 在Asp.Net WebForm的中要做到身 ...
- jQueryUI datepicker 报错: TypeError: inst is undefined
前提:要引用的js等都引用好了 调用日期控件的input: <input id="starttime" name="starttime" class=&q ...
- Alpha阶段项目复审(小小大佬带飞队)
Alpha阶段项目复审 小组的名字 优点 缺点,bug报告(至少140字) 最终名次(无并列) 只会嘤嘤嘤队 题材比较新颖!游戏和记单词的结合 有浏览器不兼容问题 5 GG队 样式新颖,自动导入好评 ...
- 如何处理App的Application的事件
http://blog.sina.com.cn/s/blog_44fa172f0102vwr2.html 直接上代码,还有条经验就是SetApplicationEventHandler可注册多个事件方 ...
- Android-CallUtil工具类
打电话相关的工具类: public final class CallUtil { /** * 拨打电话(直接拨打电话) * @param phoneNum 电话号码 */ @SuppressLint( ...
- Nodejs异步框架——async
上次的网页爬虫写完后,又打算做一个爬图的工具.前两天已经写好了代码.思路如下: 分析页面还是采用cheerio,对<div>中的img进行分析抽取,拿到图片的url.然后用childpro ...
- python - http请求带Authorization
# 背景 接入公司的一个数据统计平台,该平台的接口是带上了Authorization验证方式来保证验签计算安全 # 方法 其实很简单,就是在header中加入key=Authorization,val ...
- SQL学习笔记1
2018.10.15:周一 -- 返回前5个数据 SELECT TOP 5 * FROM Student; -- 返回前50%的数据 SELECT TOP 50 PERCENT * FROM ...