【leetcode】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.
思路:
一看这道题,我傻眼了。尼玛这么简单的题居然是中等难度。莫非有阴谋? 然后我写了个线性算法试了一下,结果完美的通过了....
class Solution {
public:
int findPeakElement(const vector<int> &num) {
int ans;
if(num.size() == ) //只有一个数字的情况
return ;
for(int i = ; i < num.size(); i++)
{
if((i == && num[i] > num[i + ])
|| (i == num.size() - && num[i] > num[i - ])
|| (num[i] > num[i + ] && num[i] > num[i - ]))
{
return i;
}
}
}
};
然后,我就去讨论圈逛去了,看看到底有什么玄机。一看,果然是有更好的方法。
可以用二分查找,得到O(logN)的算法。
题目中num[i] ≠ num[i+1] 很关键,保证了中间取的值num[mid]与 num[mid + 1]不同,从而相比于num[mid + 1],num[mid]一定位于上坡或下坡。这样不满足峰值条件时,处于上坡就把 l 收缩到mid处,处于下坡就把 r 收缩到 mid 处, 不断收缩,使得最终l与r之间只有一个峰值。
class Solution {
public:
int findPeakElement(const vector<int> &num) {
int len = num.size();
if (len == ) return ;
int left = , right = len - ;
int mid;
while (left < right - ) {
mid = left + (right - left) / ;
if (num[mid] > num[mid-] && num[mid] > num[mid+]) return mid;
if (num[mid] < num[mid-]) right = mid; // like hill climbing
else left = mid;
}
return num[left] > num[right] ? left : right;
}
};
【leetcode】Find Peak Element ☆的更多相关文章
- 【leetcode】Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- 【Leetcode】【Medium】Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 【数组】Find Peak Element
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- 【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...
- 【leetcode】Kth Largest Element in an Array (middle)☆
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 【LeetCode】169 - Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- java 反射的踩的一个坑
今天工作的时候用到了一个反射.其业务简单描述为:系统启动时将需要定时调用的方法签名保存到数据库中,开启线程定时从数据库中读取对应的方法签名,通过反射生成实例后调用方法.完成一定的定时任务. 写到的方法 ...
- mina socket底层主流程源码实现
一,mina的架构 mina 架构可以大致分为三部分,ioService ,ioFilterChain , IoHandler ioService:用于接受服务或者连接服务,例如socket 接收 ...
- CENTOS6.2系统日志rsyslog替换默认的日志服务syslog 转载自http://www.phpboy.net/linux/648.html
最近遇到配置centos 6.2的sshd及sftp日志,发现/etc/syslog.conf文件不存在, 然后: #rpm -qa | grep syslog 出来的是 rsyslog-5.8.10 ...
- Redis源码研究--redis.h
------------7月3日------------ /* The redisOp structure defines a Redis Operation, that is an instance ...
- js根据IP取得天气
<span id="weather"></span> <script> function weather(cityName) { var cha ...
- C# 查处出现次数最多的元素
LINQ var str= str.ToCharArray() .GroupBy(x => x) .OrderByDescending(x => x.Count()) .First() . ...
- sql 对一张表进行按照不同条件进行多次统计
最近一直在做数据统计,在此过程中,遇到过好多种情况都是对一张表按照不同的条件进行多次统计,以前的做法是统计几次按照不同的条件left join 几次,虽然也能得到想要的结果,但是效率太低,反映在页面就 ...
- Python之str(),repr(),``
对于对象obj: str()生成的字串是给人看的 repr()生成的字串是给解析器看的 ``与repr()等义. 最直接就是: ------------------- obj=eval(repr(ob ...
- 保持iOS上键盘出现时输入框不被覆盖
如果屏幕中的内容项目比较多,键盘就可能覆盖住文本输入框之类的对象.你必须调整你的内容,使得输入框保持可见. 你会想到哪些处理方法呢? 第一种, 临时调整窗口中各个视图的大小,使得键盘从下向上占领的区域 ...
- php中调用用户自定义函数的方法:call_user_func,call_user_func_array
看UCenter的时候有一个函数call_user_func,百思不得其解,因为我以为是自己定义的函数,结果到处都找不到,后来百度了一下才知道call_user_func是内置函数,该函数允许用户调用 ...