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 ☆的更多相关文章

  1. 【leetcode】Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  2. 【Leetcode】【Medium】Find Peak Element

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

  3. 【数组】Find Peak Element

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

  4. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

  5. 【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  6. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

  7. 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...

  8. 【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 ...

  9. 【LeetCode】169 - Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. 探索VS中C++多态实现原理

    引言 最近把<深度探索c++对象模型>读了几遍,收获甚大.明白了很多以前知其然却不知其所以然的姿势.比如构造函数与拷贝构造函数什么时候被编译器合成,虚函数.实例函数.类函数的区别等等.在此 ...

  2. 【风马一族_Java】在某个范围内,找出具有水仙花特征的数字

    打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如: 153是一个"水仙花数",因为153=1的三 ...

  3. 转载:Linux内核探索之路——关于书

    转自http://blog.chinaunix.net/uid-20608849-id-3029223.html 在学习Linux内核代码的过程中,定会参考很多书籍以及网路资源,但是并不是所有的书籍和 ...

  4. jquery实现ajax,返回json数据

    jquery实现ajax可以调用几种方法 我经常用的是$get(url,data,callback,type)方法 其中url是异步请求的页面(可以是.ashx文件),data是参数,callback ...

  5. Oracle bbed 实用示例-----修改Data内容、恢复delete的rows

    bbed 可以在db open 状态来进行修改,但是建议在做任何修改操作之前先shutdown db. 这样避免checkpoint 进程重写bbed 对block 的修改. 也避免oracle 在b ...

  6. pyQuery的安装

    1. 直接通过pip安装 你会发现lxml怎么搞都报错,后来单独先安装libxml2和libxslt pip是找不到这个包的,只好百度.发现有很多的例子的解决方案.后来发现了个实用的. 2. 先安装l ...

  7. 5、WPF实现简单计算器-非常适合初学者练习

    Sample Calculator 这是微软社区WPF的一个示例,在源程序的基础上我进行了一点点修改,非常适合初学者练习,详细代码解释. 源程序的下载地址 http://code.msdn.micro ...

  8. Oracle 的证也会过期咯

    How does this recertification requirement affect me? If your Database Certification credential is re ...

  9. Redis 二:入门基本篇

    .多数据库设置 - ,代表16个数据库 .glob风格通配符 keys * 返回所有 keys ba? 返回 bar keys b[a-z][a-z] 返回btt bar \x 匹配转义字符 .判断一 ...

  10. Mysql 创建数据库表(删除,删除,插入)

    MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (col ...