题目地址:

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.

click to show spoilers.

Note:

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 解题报告的更多相关文章

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

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

  2. LeetCode: Find Peak Element 解题报告

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

  3. 【原创】leetCodeOj --- Sliding Window Maximum 解题报告

    天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...

  4. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  5. 【原创】leetCodeOj --- Jump Game II 解题报告

    原题地址: https://oj.leetcode.com/problems/jump-game-ii/ 题目内容: Given an array of non-negative integers, ...

  6. 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告

    原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...

  7. 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)

    原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...

  8. 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告

    原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...

  9. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

随机推荐

  1. HNCU1101:马的移动(BFS)

    http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1101 题目描述 小明很喜欢下国际象棋,一 ...

  2. ubuntu下海信Hisense E920 usb连接不上的处理与adb的连接

    解决lssub未能发现海信Hisense USB设置:选择 天翼宽带连接 如下所示: luogw@luogw-ThinkPad-Edge:~$ lsusb Bus 001 Device 002: ID ...

  3. hdu1495之经典搜索

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. winXP JDK由1.8改为1.6

    (1)直接在环境变量中删除配置的相关路径 path的值: C:\Documents and Settings\Administrator>path PATH=C:\Documents and S ...

  5. Centos 6安装完美搭建mysql、php、apache之旅

    安装apache [root@centos share]# yum -y install httpd Loaded plugins: fastestmirror, refresh-packagekit ...

  6. hdu1535(最短路)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1535 题意:给你一个源点,让你从这里派发n个学生去其余的n-1个站点去邀请人们去CSS,然后再返回CS ...

  7. birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典

    birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典 birdnest

  8. 利用objc的runtime来定位次线程中unrecognized selector sent to instance的问题

    昨天遇到一个仅仅有一行错误信息的问题: -[NSNull objectForKey:]: unrecognized selector sent to instance 0x537e068 因为这个问题 ...

  9. Notifications(通知)

    通知 通知是能在应用的普通用户界面外显示给用户的一种消息. 当你告诉系统公布一条通知时,它首先在通知栏中表现为一枚图标. 用户打开通知抽屉后就能查看通知的细节了. 通知栏和通知抽屉都是由系统控制的区域 ...

  10. java解析String类型t复杂xml,多级节点,最好的例子

    需要用jar包 dom4j-1.6.1.jar 字符串xml如下: <root> <flw> <name>aa</name> <age>22 ...