【原创】leetCodeOj --- Find Peak Element 解题报告
题目地址:
https://oj.leetcode.com/problems/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.
Your solution should be in logarithmic complexity.
方法:
有两点需要注意的:
0、复杂度要为O(log n)
1、注意审题
我第一次做,以为要返回所有peak element,想破头都找不到一个log复杂度的办法,后来读题才发现是让你找一个就行。
那就单纯了
因为只要你找一个,所以首先看中间元素是不是,如果是直接返回中间那个,如果不是就跟着比中间大的元素那边走,砍掉另一半。如果两个都比中间元素大就随意走一边。
证明也比较简单。
往大的那边走的区域肯定存在一个peak element,分两种情况
0、全是大于号:那么就是该区域最后一个值。
1、有小于号:至少有一个大于号和小于号配对,即存在peak element
因此,大的区域那边总会有peak element。
不明白的同学留言
全部代码:
class Solution {
public:
int findPeakElement(const vector<int> &num) {
if (num.size() == )
return ;
return trueStuff(num,,num.size() - );
}
int trueStuff(const vector<int> &num,int start,int fin)
{
int mid = (start + fin) / ;
if (mid - < )
return num[mid] > num[mid+] ? mid : mid + ;
if (mid + >= num.size())
return num[mid] > num[mid-] ? mid : mid - ;
if (num[mid-] < num[mid] && num[mid+] < num[mid])
return mid;
return num[mid] > num[mid-] ? trueStuff(num,mid + ,fin) : trueStuff(num,start,mid - );
}
};
【原创】leetCodeOj --- Find Peak Element 解题报告的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- LeetCode: Find Peak Element 解题报告
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- 【原创】leetCodeOj --- Sliding Window Maximum 解题报告
天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...
- 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)
题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...
- 【原创】leetCodeOj --- Jump Game II 解题报告
原题地址: https://oj.leetcode.com/problems/jump-game-ii/ 题目内容: Given an array of non-negative integers, ...
- 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告
原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...
- 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)
原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...
- 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告
原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
随机推荐
- 深入探讨:LBS是一种工具而非一种模式
移动互联网的快速发展,带动着移动互联网应用的不断创新.从2010起,LBS的概念就在中国迅速兴起,但到了2011年底提供LBS服务的企业从最多50家已经降至仅剩15家.投行在看好移动互联网的同时又对L ...
- 简单的三方登录SDK示例,Android Activity之间数据的传递
先建立Library工程,即普通工程然后在Android的属性勾选Library选项. 这里建立的工程为 mySdk ,Activity名为LoginActivity. LoginActivity代码 ...
- Linux内核升级
一.测试环境 CentOS6.5 X86 64位 内核版本为 2.6.32 VM 10.07 二.编译内核版本 2.1.kernel 3.2.71 2.2.kernel 3.4.108 2.3.ker ...
- selenium2支持无界面操作(HtmlUnit和PhantomJs)
selenium2支持无界面操作(HtmlUnit和PhantomJs) selenium2支持通过各种driver(FirfoxDriver,IternetExplorerDriver,OperaD ...
- 在Mybatis中使用注解@多个參数查询
@Select("SELECT * FROM wc_homework WHERE organization_id=#{classId} ORDER BY createtime DESC LI ...
- [leetcode]3 Sum closest
问题叙述性说明: Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- 使用cacti监控服务器
一.cacti安装 cacti需要lamp环境,因此需要首先编译安装lamp环境,安装完成后,下载cacti的zip包,解压到/www/htdocs目录下,创建cacti需要的数据库. 完成上述后,在 ...
- C#反射机制详解(转)
两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到的呢?B超是B型超声波,它可以透过肚皮通过向你体内发射B型超声波,当超声波遇到内脏壁的时 ...
- 怎样改动SVN的地址
改动svn地址的目的有两个,一个是更改默认svn路径.还有一个就是svn库server迁移了. 我碰到的是另外一种情况,SVN的IP地址改了,须要这么切换: 在本地配置库副本根文件夹点击鼠标右键--& ...
- 《Linux设备驱动开发具体解释(第3版)》进展同步更新
本博实时更新<Linux设备驱动开发具体解释(第3版)>的最新进展. 2015.2.26 差点儿完毕初稿. 本书已经rebase到开发中的Linux 4.0内核,案例多数基于多核CORTE ...