题目地址:

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. hdu 5090 Game with Pearls

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 题意:n个数,k,给n个数加上k的正倍数或者不加,问最后能不能凑成1 到 n的序列 题目分类:暴 ...

  2. docker 私有仓库查询

    docker:/etc/init.d# docker ps -a | grep reg c7d5592a3658 registry "docker-registry" 11 min ...

  3. Dvtm -- 平铺式终端

    Dvtm -- 平铺式终端_Linux伊甸园开源社区-24小时滚动更新开源资讯,全年无休! Dvtm -- 平铺式终端 关注我们: 如果你使用过 Dwn 或 Awesome 这种平铺式窗口管理器,一定 ...

  4. 端口映射工具 redir/socat/xinetd - 运维技术 - 开源中国社区

    端口映射工具 redir/socat/xinetd - 运维技术 - 开源中国社区 端口映射工具 redir/socat/xinetd    10人收藏此文章, 我要收藏 发表于3天前(2013-08 ...

  5. swift-switch使用方法

    // Playground - noun: a place where people can play import UIKit //--------------------------------- ...

  6. 检查Linux Bash安全漏洞以及各环境修复解决方法

    第一.检测是否存在Bash漏洞$env x='() { :;}; echo vulnerable' bash -c "echo this is a test"如果返回以下内容:则请 ...

  7. c#抓取动态页面WebBrowser

    在ajax横行的年代,很多网页的内容都是动态加载的,而我们的小爬虫抓取的仅仅是web服务器返回给我们的html,这其中就 跳过了js加载的部分,也就是说爬虫抓取的网页是残缺的,不完整的,下面可以看下博 ...

  8. JavaScript 使用Document记录cookie

    cookie对于我们使用者来说,有时帮助还是挺大的,比方对于一些不是特别重要的站点,比方公司的測试平台,每次登陆都要手动输入username和password 非常繁琐.所以为了更少的引入其他框架,就 ...

  9. Redis 安装与简单示例(转)

    本文转载自:http://www.cnblogs.com/kissdodog/p/3570984.html Redis的安装 Redis下载地址如下:https://github.com/dmajki ...

  10. Storm-0.9.2-incubating源代码编译打包

    近期遇到一些同学询问Storm-0.9.2-incubating源代码编译打包的问题,现将编译步骤说明例如以下: 1.凝视掉project各pom文件里关于maven插件(maven-gpg-plug ...