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.

Note:

Your solution should be in logarithmic complexity.

利用二分法才能达到logarithmic的复杂度

当num[mid]<num[mid+1]时,peek一定在右边

当num[mid]>num[mid+1]时,peek一定在左边

 class Solution {

 public:

     int findPeakElement(const vector<int> &num) {

         int n=num.size();

         int left=;

         int right=n-;

         int result;

         int mid;       

         while(left<right)

         {

             mid=(left+right)/;

             if(mid+>n-)

             {

                 return mid;

             }           

             if(num[mid]<num[mid+])

             {

                 left=mid+;  

             }

             else if(num[mid]>num[mid+])

             {

                 right=mid;

             }             

         }

         return left;       

     }

 };

【leetcode】Find Peak Element的更多相关文章

  1. 【leetcode】Find Peak Element ☆

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  2. 【Leetcode】【Medium】Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  3. 【数组】Find Peak Element

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

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

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

  5. 【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  6. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

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

  7. 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...

  8. 【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 ...

  9. 【LeetCode】169 - Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. Java调试

    线上load高的问题排查步骤是: 先用top找到耗资源的进程 ps+grep找到对应的java进程/线程 jstack分析哪些线程阻塞了,阻塞在哪里 jstat看看FullGC频率 jmap看看有没有 ...

  2. python mysql

    mysql Linux 安装mysql: apt-get install mysql-server 安装python-mysql模块:apt-get install python-mysqldb Wi ...

  3. springmvc的form标签

    1.要使用Spring MVC提供的表单标签,首先需要在视图页面添加: <%@ taglib prefix="form" uri="http://www.sprin ...

  4. linux下如何查看chm文件

    转自:http://www.cnblogs.com/jesseZh/p/4036811.html (64位)    sudo dpkg -i chmsee_1.3.0-2ubuntu2_amd64.d ...

  5. Java学习注意事项

    一个Java文件中可以包含多个类. 如果有public类,则文件名必须和public类一样. 例如: class Pie { void f(){ System.out.println("Pi ...

  6. jQuery.imgLazyLoad图片懒加载组件

    一.前言 当一个页面中请求的图片过多,而且图片太大,页面访问的速度是非常慢的,对用户的体验非常不友好:使用图片懒加载,可以减轻服务器的压力,增加页面的访问量,这里主要是总结一下我自己写的图片懒加载组件 ...

  7. 网站SEO优化之添加Sitemap文件。

    Sitemap.xml 故名思意就是站点地图文件,可以指引Google spider 收录相应网页.正确地使用Google Sitemap,可以确保让Google spider 不遗漏网站内的任何页面 ...

  8. java 练手 Fibonacci数

    Problem B Fibonacci数 时间限制:3000 ms  |  内存限制:65535 KB   描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列 ...

  9. 输入三个数a,b,c,要示按由小到大的顺序输出

    #include<stdio.h>int main(){       double a,b,c,t;       scanf("%lf %lf %lf",&a, ...

  10. tc 147 2 PeopleCircle(再见约瑟夫环)

    SRM 147 2 600PeopleCircle Problem Statement There are numMales males and numFemales females arranged ...