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. 学习vue.js 第一天

    最近听到很多人都在用Vue.js ,我也想凑凑热闹,来个入门 啥的 ,要不以后人家说,啥都不知道,多low 看到官网 是这样介绍Vue.js Vue.js(读音 /vjuː/, 类似于 view) 是 ...

  2. jquery $.extend()扩展插件获取焦点或失去焦点事件

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 搭建自己的网站之一:如何在linux下使用tomcat

    1. 要打造自己的网站,首先应该有属于自己的服务器.个人的话可以在阿里云或者腾讯云购买云主机,需要的话再购买一个域名,与自己的公网ip绑定(不然只能由ip地址访问,当然,自己用来练手的话是没什么问题的 ...

  4. 神经网络与深度学习(3):Backpropagation算法

    本文总结自<Neural Networks and Deep Learning>第2章的部分内容. Backpropagation算法 Backpropagation核心解决的问题: ∂C ...

  5. 在页面的el表达式是如何判断null的

    <c:if test="${not empty message}"> <div id="message" class="alert ...

  6. PHP编码规范PSR-2

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  7. ios 快速审核

    https://developer.apple.com/contact/app-store/?topic=expedite

  8. 如果mac电脑的usb转接器连接wlan时不显示,也就是不识别usb此时的网络连接没有,解决办法就是如下

    1.接上电源   关机 先按下shift +ctrl + opt + 开机键    ,等待10秒,这10秒是没有反应的,屏幕不会亮,系统不会跑起来,  10秒之后松开所有键,再按下opt + cmd ...

  9. Z字形扫描(201412-2)

    问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...

  10. JavaScript数据存取的性能问题

    JavaScript中四种基本的数据存取位置: 字面量:只代表自身 字符串.数字.布尔值.对象.函数.数组.正则,以及null和undefined    快 本地变量:var定义的    快 数组元素 ...