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.

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.

click to show spoilers.

Note:

Your solution should be in logarithmic complexity.

这题就是求序列最大值。顺序查找或二分查找均可。

满足复杂度要求的话需要用二分查找。

解法一:顺序查找

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

解法二:二分查找(递归)

class Solution {
public:
int findPeakElement(vector<int>& nums) {
return Helper(nums, , nums.size()-);
}
int Helper(vector<int>& nums, int low, int high)
{
if(low == high)
return low;
int mid = low + (high-low)/;
if(nums[mid] > nums[mid+])
return Helper(nums, low, mid);
else
return Helper(nums, mid+, high);
}
};

解法三:二分查找(迭代)

class Solution {
public:
int findPeakElement(vector<int>& nums) {
int low = ;
int high = nums.size()-;
while(low < high)
{
int mid = low + (high-low)/;
if(nums[mid] > nums[mid+])
high = mid;
else
low = mid+;
}
return low;
}
};

【LeetCode】162. Find Peak Element (3 solutions)的更多相关文章

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

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

  2. 【刷题-LeetCode】162 Find Peak Element

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

  3. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  4. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  5. LeetCode OJ 162. Find Peak Element

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

  6. 【LeetCode】230. Kth Smallest Element in a BST

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...

  7. 【原创】leetCodeOj --- Find Peak Element 解题报告

    题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...

  8. 【Lintcode】075.Find Peak Element

    题目: There is an integer array which has the following features: The numbers in adjacent positions ar ...

  9. 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...

随机推荐

  1. MariaDB 主从复制

    MySQL Replication:NySQL复制,MySQL的复制默认为异步工作模式    mysql的复制功能是mysql内置的,装上它之后就具备了这个功能,而mysql复制是mysql实现大规模 ...

  2. mui中a标签的跳转问题

    一.脑补 快速响应是mobile App实现的重中之重,研究表明,当延迟超过100毫秒,用户就能感受到界面的卡顿,然而手机浏览器的click点击存在300毫秒延迟(至于为何会延迟,及300毫秒的来龙去 ...

  3. PHP格式化金钱函数

    实现目的: 对数字进行格式化,以类似¥10,000,000的格式输出. 实现方法: function doFormatMoney($money){ $tmp_money = strrev($money ...

  4. Ubuntu 11.04安装GCC 4.6.1

    首先下载相应的源代码:ftp://ftp.dti.ad.jp/pub/lang/gcc/releases/gcc-4.6.1/#下载 gcc-4.6.1.tar.bz2 ftp://ftp.dti.a ...

  5. shell之if简化语句

    最常用的简化if语句: && 如果是“前面”,则“后面” [ -f /var/run/dhcpd.pid ] && rm /var/run/dhcpd.pid 检查 文 ...

  6. 【翻译自mos文章】job 不能自己主动运行的解决方法

    job 不能自己主动运行的解决方法 參考原文: Jobs do not execute automatically (Doc ID 309945.1) 适用于: Oracle Server - Ent ...

  7. SWT常用组件

    SWT类所代表的事件常量: 事件类型常量 说明 SWT.Activate 当激活窗口时 SWT.Arm 菜单项被选中之前 SWT.Close 关闭窗口时 SWT.Collapse 折叠树的节点时 SW ...

  8. MS Sql Server 中主从库的配置和使用介绍

    网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库. 主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据 ...

  9. iOS开源项目:AudioPlayer

    AudioPlayer是一个基于AVAudioStreamer的在线音乐播放软件. https://github.com/marshluca/AudioPlayer 首先将歌曲信息存储在NSArray ...

  10. Spring Bean 注入 2 注解篇

    1. 自动装配注解 配置applicationContext.xml开启注解 <?xml version="1.0" encoding="UTF-8"?& ...