Problem:

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.

Your solution should be in logarithmic complexity.

Summary:

找到数组中的局部最大数。

Solution:

1. 顺序查找:最直接的方法,复杂度为O(n)

 class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
if (len == ) {
return ;
} for (int i = ; i < len; i++) {
if (!i && nums[i] > nums[i + ] ||
i == len - && nums[i] > nums[i - ] ||
nums[i] > nums[i - ] && nums[i] > nums[i + ]) {
return i;
}
} return -;
}
};

2. 二分查找:首先找到整体的中间值m,若m符合局部最大条件则返回m,否则若nums[m - 1] > nums[m]则在[0, m - 1]中查找。因为数组左边和右边为负无穷,所以在这种情况下[0, m - 1]中一定存在一个局部最大值。这种方法复杂度为O(logn)。

 class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
if (len == ) {
return ;
} int l = , r = len - ;
while (l <= r) {
int m = (l + r) / ;
if ((!m || nums[m] >= nums[m - ]) &&
(m == len - || nums[m] >= nums[m + ])) {
return m;
}
if (m && nums[m] < nums[m - ]) {
r = m - ;
}
else {
l = m + ;
}
} return -;
}
};

二分查找的简略写法:

 class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
if (len == ) {
return ;
} int l = , r = len - ;
while (l < r) {
int m = (l + r) / ;
if (nums[m] > nums[m + ]) {
r = m;
}
else {
l = m + ;
}
} return r;
}
};

LeetCode 162 Find Peak Element的更多相关文章

  1. LeetCode 162. Find Peak Element (找到峰值)

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

  2. (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element

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

  3. [LeetCode] 162. Find Peak Element 查找峰值元素

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

  4. Java for LeetCode 162 Find Peak Element

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

  5. ✡ leetcode 162. Find Peak Element --------- java

    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(二分法)

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

  7. LeetCode 162.Find Peak Element(M)(P)

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

  8. LeetCode——162. Find Peak Element

    一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...

  9. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

随机推荐

  1. 用MySQL语法建 一个学生表,包括学生姓名、性别、年龄、班级信息。

    (1)创建表的SQL语句 create table student ( ID int primary key not null, NAME varchar(50), sex int, age int, ...

  2. 【翻译】XV6-DRAFT as of September 3,2014 第0章 操作系统接口

    操作系统接口 操作系统的任务是让多个程序共享计算机(资源),并且提供一系列基于计算机硬件的但更有用的服务.操作系统管理并且把底层的硬件抽象出来,举例来说,一个文字处理软件(例如word)不需要关心计算 ...

  3. [bzoj3626][LNOI2014]LCA

    Description 给出一个$n$个节点的有根树(编号为$0$到$n-1$,根节点为$0$). 一个点的深度定义为这个节点到根的距离$+1$. 设$dep[i]$表示点$i$的深度,$lca(i, ...

  4. PHP build notes - WARNING: This bison version is not supported for regeneration of the Zend/PHP parsers (found: 3.0, min: 204, excluded: 3.0).

     WARNING: This bison version is not supported for regeneration of the Zend/PHP parsers (found: 3.0, ...

  5. 2 云计算系列之KVM的安装与使用

    preface 在上篇博客中,我们讲了云的概念,分类,以及虚拟化技术.我们知道Openstack的虚拟化技术是基于KVM的,所以下面就开始说说如何部署和使用KVM. 下面的讲解包含以下知识点: 安装K ...

  6. layer弹出框小结

    1.layer.open() // 1.打开弹出层 layer.open({ type:1, //基本层类型 icon:, //图标 content:'请核对信息!', //内容 shade:0.3, ...

  7. 聊一聊PV和并发

    最近和几个朋友,聊到并发和服务器的压力问题.很多朋友,不知道该怎么去计算并发?部署多少台服务器才合适? 所以,今天就来聊一聊PV和并发,还有计算web服务器的数量 的等方法.这些都是自己的想法加上一些 ...

  8. phpcms 添加memcache支持

    1,修改caches/configs/cache.php <?php return array ( 'file1' => array ( 'type' => 'file', 'deb ...

  9. 【IDEA 2016】intellij idea tomcat jsp 热部署

    刚开始用IDEA,落伍的我,只是觉得IDEA好看.可以换界面.想法如此的low. 真是不太会用啊,弄好了tomcat.程序启动竟然改动一下就要重启,JSP页面也一样. IDEA可以配置热部署,打开to ...

  10. JavaScript原型

    prototype与_proto_ 对象的 prototype 属性的方法.属性为对象所属的那一"类"所共有.对象原型链通过 proto 属性向上寻找. 为 proto 指定 nu ...