【leetcode】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.
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 ☆的更多相关文章
- 【leetcode】Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- 【Leetcode】【Medium】Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 【数组】Find Peak Element
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- 【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...
- 【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 ...
- 【LeetCode】169 - Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- HTML文件中使用Java程序
HTML文件中使用Java程序:简而言之,在HTML文件中引入java应用程序,并通过javascript调用其方法. 一. 运行环境 1.JAVA_HOME.CLASSPATH.PATH配置正确 ...
- Z-Stack学习笔记
Technorati 标签: Z-Stack profile 1. 栈配置profile 栈参数的集合需要被配置为一定的值,连同这些值在一起被称之为栈配置.ZigBee联盟定义了这些由栈配置组成的栈参 ...
- SPL标准库常用的数据结构
栈数据结构 $stack = new SplStack(); //栈数据结构->先进后出 2 $stack->push('data1'); //入栈 $stack->push('da ...
- java基础-浅复制与深复制的理解
浅复制与深复制在很多编程语言中都有出现,那么什么是浅复制,什么是深复制呢? 要区分浅复制与深复制,首先我们要明确什么是复制,怎样才算是复制.复制的例子在生活中也随处可见,如复印一份文档,复制一段文字等 ...
- 4.Servlet_Form表单处理
1.建项目"3Servlet_Form",src下建包“com.amaker.servlet”,web-root下建Register.html <!DOCTYPE html& ...
- 解决iOS设备屏幕切换时页面造成的问题
环境:IOS6~7 Safari 问题:iOS设备屏幕切换时可能会造成屏幕变大,出现左右间距等问题 解决方法: 头部加入<meta name = "viewport" con ...
- SOAP 介绍
简介 SOAP(Simple Object Access Protoco)简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于 XML 的协议.此协议规范由 IBM.Microso ...
- iphone document 图片存储和读取
转载自:http://longquan.iteye.com/blog/1669990 存: //此处首先指定了图片存取路径(默认写到应用程序沙盒 中) NSArray *paths = NSSearc ...
- iis 下的 selfssl
当然,如果你想省掉所有这些麻烦也行,最简单的在IIS启动SSL的方法只要3步: 1. 下载 IIS 6.0 Resource Kit Tools: http://www.microsoft.com/d ...
- VS2010性能监视工具
<编程珠玑(续)>第一章中就介绍了性能监视工具,对于较简单的程序来说,性能监视工具其实可以用变量累加来计算的,但是对于较复杂的程序来说就需要比较好的性能监视工具了.而VS2010提供了一个 ...