题目地址:

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. Delphi5 update1的序列号

    好不容易找到一个: Serial Number: 100-006-1659Key: 6ax0-91x0 ------------------------------------------- 办法2: ...

  2. POJ 3415 Max Sum of Max-K-sub-sequence (线段树+dp思想)

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. 解压tar.gz文件报错gzip: stdin: not in gzip format解决方法

    解压tar.gz文件报错gzip: stdin: not in gzip format解决方法 在解压tar.gz文件的时候报错 1 2 3 4 5 [Sun@localhost Downloads] ...

  4. 整理自百度知道提问的几道Java编程题

    蚂蚁爬杆 问题描述: 有一根27厘米的细木杆,在第3厘米.7厘米.11厘米.17厘米.23厘米这五个位置上各有一只蚂蚁.木杆很细,不能同时通过一只蚂蚁.开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝 ...

  5. CMake Intro - CMakeLists.txt

    Notes:  directory structure:  cmake, cmake/Tutorial, cmake/Tutorial/MathLibs 1. File lists in cmake/ ...

  6. 从零开始学C++之动态创建对象

    回顾前面的文章,实现了一个简单工厂模式来创建不同类对象,但由于c++没有类似new "Circle"之类的语法,导致CreateShape 函 数中需要不断地ifelse地去判断, ...

  7. hdu5171(矩阵快速幂)

    传送门:GTY's birthday gift 题意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法 ...

  8. Android.mk编译.apk .so .jar .a第三方.apk .so .jar .a的方法

    一.编译一个简单的APK LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java s ...

  9. TMS320F28335项目开发记录9_28335中断系统

    28335中断系统 1.中断系统 在这里我们要十分清楚DSP的中断系统. C28XX一共同拥有16个中断源,当中有2个不可屏蔽的中断RESET和NMI.定时器1和定时器2分别使用中断13和14.这样还 ...

  10. JS验证身份证的合法性

    //验证身份证的合法性 function IdentityCodeValid(code) { var city={11:"北京",12:"天津",13:&quo ...