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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Hide Tags

Array Binary Search

 

 
    这题其实是二分搜索的变形,考虑清楚 left  mid  right  取值的情况,结合判断 mid及其左右构成的升降序来剪枝。
 
 
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Solution {
  6. public:
  7. int findPeakElement(const vector<int> &num) {
  8. int n = num.size();
  9. if(n==&&num[]<num[]&&num[]>num[]) return ;
  10. if(n<) return ;
  11. if(num[]>num[]) return ;
  12. if(num[n-]<num[n-]) return n-;
  13. return help_fun(num,,num.size()-);
  14. }
  15. int help_fun(const vector<int> &num,int lft,int rgt)
  16. {
  17. int n = num.size();
  18. if(rgt - lft <){
  19. if(lft!=&&num[lft-]<num[lft]&&num[lft]>num[lft+])
  20. return lft;
  21. if(rgt!=n-&&num[rgt-]<num[rgt]&&num[rgt]>num[rgt+])
  22. return rgt;
  23. return -;
  24. }
  25. int mid = (lft+rgt)/;
  26. if(num[mid-]<num[mid]&&num[mid]>num[mid+]) return mid;
  27. int ascend = -;
  28. if(num[mid-]<num[mid]&&num[mid]<num[mid+]) ascend = ;
  29. if(num[mid-]>num[mid]&&num[mid]>num[mid+]) ascend = ;
  30. if(ascend==&&num[mid]>=num[rgt])
  31. return help_fun(num,mid,rgt);
  32. if(ascend==&&num[lft]<-num[mid])
  33. return help_fun(num,lft,mid);
  34. int retlft = help_fun(num,lft,mid);
  35. if(retlft!=-) return retlft;
  36. return help_fun(num,mid,rgt);
  37. }
  38. };
  39.  
  40. int main()
  41. {
  42. vector<int > num{,,};
  43. Solution sol;
  44. cout<<sol.findPeakElement(num)<<endl;
  45. return ;
  46. }

[LeetCode] Find Peak Element 二分搜索的更多相关文章

  1. LeetCode Find Peak Element [TBD]

    说要写成对数时间复杂度,算了想不出来,写个O(n)的水了 class Solution { public: int findPeakElement(const vector<int> &a ...

  2. [LeetCode] Find Peak Element 求数组的局部峰值

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

  3. LeetCode Find Peak Element

    原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...

  4. LeetCode Find Peak Element 找临时最大值

    Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...

  5. LeetCode: Find Peak Element 解题报告

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

  6. Lintcode: Find Peak Element

    There is an integer array which has the following features: * The numbers in adjacent positions are ...

  7. LeetCode OJ 162. Find Peak Element

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

  8. LeetCode 162. Find Peak Element (找到峰值)

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

  9. (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

随机推荐

  1. Firebase Cloud Function 编写与部署

    1.设置和初始化 Firebase SDK for Cloud Functions (1).Cloud Functions 运行的是 Node v6.14.0,因此需要安装nodejs: https: ...

  2. python正则表达式入门篇

    文章来源于:https://www.cnblogs.com/chuxiuhong/p/5885073.html Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. ...

  3. 12,scrapy框架之post请求

    今日概要 递归爬取解析多页页面数据 scrapy的post请求发送 1.递归爬取解析多页页面数据 - 需求:将糗事百科所有页码的作者和段子内容数据进行爬取切持久化存储 - 需求分析:每一个页面对应一个 ...

  4. WPF实现QQ群文件列表动画(一)

    QQ群大家都用过,先看下目前QQ的群文件列表容器的效果: 细心点大家就会发现,这玩意收缩和展开是带动画的,并不是很僵硬地直接收缩或者直接展开,毫无疑问,如果用WPF实现这样的效果,这里的最佳控件是Ex ...

  5. 【精彩回顾】第二届微医前端技术沙龙(附PPT下载)

    5 月 25 日,以「无界」为主题的第二届微医前端技术沙龙成功举办.本届沙龙的演讲题目涵盖了前端技术几个主要的应用场景,包括服务端.桌面端以及跨平台的开发.最近几年前端技术发展非常快,各种可以提高开发 ...

  6. laravel5.2总结--blade模板

    ## 1.基本用法 ``` ##情形1 $name = laravel5 <div class="title"> {{$name}} {{$name}}</div ...

  7. IOS开发---菜鸟学习之路--(十二)-利用ASIHTTPRequest进行异步获取数据

    想要实现异步获取的话我这边了解过来有两个非常简单的方式 一个是利用ASIHTTPRequest来实现异步获取数据 另一个则是利用MBProgressHUD来实现异步获取数据 本章就先来讲解如何利用AS ...

  8. SpringBoot中Async异步方法和定时任务介绍

    1.功能说明 Spring提供了Async注解来实现方法的异步调用. 即当调用Async标识的方法时,调用线程不会等待被调用方法执行完成即返回继续执行以下操作,而被调用的方法则会启动一个独立线程来执行 ...

  9. application.properties 详解

    mvc spring.mvc.async.request-timeout设定async请求的超时时间,以毫秒为单位,如果没有设置的话,以具体实现的超时时间为准,比如tomcat的servlet3的话是 ...

  10. JAVA 程序监控基础简述

    最近在项目中自感程序木有问题,也没有什么错误日志出来.但就是有人反映服务慢,有时连不上的情况.为了解决这么妖的问题只能去详细的看看运行中的程序到底出了什么情况,这时如果有个比较好的监控工具可以监控运行 ...