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及其左右构成的升降序来剪枝。
 
 
#include <vector>
#include <iostream>
using namespace std; class Solution {
public:
int findPeakElement(const vector<int> &num) {
int n = num.size();
if(n==&&num[]<num[]&&num[]>num[]) return ;
if(n<) return ;
if(num[]>num[]) return ;
if(num[n-]<num[n-]) return n-;
return help_fun(num,,num.size()-);
}
int help_fun(const vector<int> &num,int lft,int rgt)
{
int n = num.size();
if(rgt - lft <){
if(lft!=&&num[lft-]<num[lft]&&num[lft]>num[lft+])
return lft;
if(rgt!=n-&&num[rgt-]<num[rgt]&&num[rgt]>num[rgt+])
return rgt;
return -;
}
int mid = (lft+rgt)/;
if(num[mid-]<num[mid]&&num[mid]>num[mid+]) return mid;
int ascend = -;
if(num[mid-]<num[mid]&&num[mid]<num[mid+]) ascend = ;
if(num[mid-]>num[mid]&&num[mid]>num[mid+]) ascend = ;
if(ascend==&&num[mid]>=num[rgt])
return help_fun(num,mid,rgt);
if(ascend==&&num[lft]<-num[mid])
return help_fun(num,lft,mid);
int retlft = help_fun(num,lft,mid);
if(retlft!=-) return retlft;
return help_fun(num,mid,rgt);
}
}; int main()
{
vector<int > num{,,};
Solution sol;
cout<<sol.findPeakElement(num)<<endl;
return ;
}

[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. 六、Linux 文件基本属性

    Linux 文件基本属性 Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规 ...

  2. swoole 连接池

    proxy_pool.php <?php class ProxyServer { protected $frontends; protected $backends; /** * @var sw ...

  3. Base64及其Python实现

    1. 什么是Base64 Base64是一种基于64个可打印字符来表示二进制数据的表示方法 Base64是一种编码方式,提及编码方式,必然有其对应的字符集合.在Base64编码中,相互映射的两个集合是 ...

  4. 20190103(GIL,池,阻塞,同步异步)

    GIL锁 什么是GIL GIL全局解释器锁,是防止多个线程在同一时间同时执行的.CPython解释器特有的一种互斥锁. 每一个py文件都会有自己的解释器,也就是说不同py文件的GIL都是独立的, ps ...

  5. 添加SQL字段

    通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数增加字段: alter table [表名] add 字段名 smallin ...

  6. Dire Wolf HDU - 5115(区间dp)

    Dire Wolf Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total ...

  7. Session只读的影响

    .net中使用Session必须实现IRequiresSessionState接口,不过还有个只读的接口IReadOnlySessionState, 若是实现只读的接口,那么在该页面(如一般处理程序) ...

  8. Python linecache模块

    Table of Contents 1. linecache 1.1. 其它 2. 参考资料 linecache 今天分享一个python的小模块: linecache, 可以用它方便地获取某一文件某 ...

  9. Git的安装及常用操作

    一.Git的安装 1.下载Git,官网地址为:https://git-scm.com/downloads.     2.下载完成之后,双击目录进行安装 3.选择安装目录 4.选择组件,默认即可 5.设 ...

  10. MySQL基础3-SQL语言

    1.DQL语句分类 重点在于Select语句 2.Sql语句的书写规则 3.怎样使用Navicat导入已经写好的sql文件 (1)在Navicat中右击选中的数据库 (2)将sql文件所在的路径添加进 ...