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. 基础学习总结(八)--HttpClient

    在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端.它提供了对HTTP协议的全面支持,可以使用HttpClient的对象来执行HTTP GET ...

  2. Winform ListView根据条件定位到指定行

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. C# ProperTyGrid 自定义属性

    C# 如果要实现自定义属性必须要需要实现接口ICustomTypeDescriptor // 摘要:   //     提供为对象提供动态自定义类型信息的接口.   public interface ...

  4. Yii2框架数据库增删改查小结

    User::find()->all();    //返回所有用户数据:User::findOne($id);   //返回 主键 id=1  的一条数据: User::find()->wh ...

  5. PHP获取当前日期或指定日期的所在月的第一天和最后一天

    <?php function getthemonth($date) { $firstday = date('Y-m-01', strtotime($date)); $lastday = date ...

  6. CentOS6.3连网获取IP失败 This device is not active

    虚拟机拷贝到其它机器之后,启动:然后用ifconfig -a发现eth0没有IP地址,查看 /etc/sysconfig/network-scripts/ifcfg-eth0文件,发现IP地址已经指定 ...

  7. 管理口令(P):[INS-30001] ADMIN口令为空之Oracle安装

    在安装oracle database11g 发行版的时候出现下面这个问题. 无论怎么输入密码都提示有问题,都输入得鬼火了!去百度了一下,果然有命名规则的 规则如下:小写字母+数字+大写字母

  8. MySQL监控工具-orzdba

    源代码地址:http://code.taobao.org/p/orzdba/src/trunk/     [root@hank-yoon servers]# chmod +x orzdba 在代码的1 ...

  9. Ubuntu中设置环境变量详解

    1, 为单一用户:.bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.打开用户主目录下的.bashrc,在这个文件中加入export PA ...

  10. SOS.dll(SOS 调试扩展)

      SecAnnotate.exe(.NET 安全批注器工具) SignTool.exe(签名工具) Sn.exe(强名称工具) SOS.dll(SOS 调试扩展)   SqlMetal.exe(代码 ...