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. 安全 - 堡垒机 - Jumpserver

    GitHub - jumpserver/Dockerfile: Jumpserver all in one Dockerfile https://github.com/jumpserver/Docke ...

  2. css清除浮动,清除子节点margin溢出问题

    清除浮动 .clearfix:after{ content:”.”; display:block; height:0; clear:both; visibility:hidden; } 清除margi ...

  3. JZOJ 5462. 【NOIP2017提高A组冲刺11.8】好文章

    5462. [NOIP2017提高A组冲刺11.8]好文章 (File IO): input:article.in output:article.out Time Limits: 1000 ms  M ...

  4. day 44 前端HTML

    前端HTML   HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk ...

  5. haystack(django的全文检索模块)

    haystack haystack是django开源的全文搜索框架 全文检索:标题可以检索,内容也可以检索 支持solr ,elasticsearch,whoosh 1.注册app 在setting. ...

  6. devicemaps_init(mdesc)

    devicemaps_init的参数为machine_desc结构体.以s3c6410为例,在arch/arm/mach-s3c64xx/mach-smdk6410.c中使用上述宏声明machine_ ...

  7. [原]sencha touch之NavigationView

    还是直接上代码,都是基本的几个容器控件,没什么还说的 Ext.application({ name:'itkingApp', launch:function(){ var view =Ext.crea ...

  8. CodeForces 781E Andryusha and Nervous Barriers 线段树 扫描线

    题意: 有一个\(h \times w\)的矩形,其中有\(n\)个水平的障碍.从上往下扔一个小球,遇到障碍后会分裂成两个,分别从障碍的两边继续往下落. 如果从太高的地方落下来,障碍会消失. 问从每一 ...

  9. Redis实现之整数集合

    整数集合 整数集合(insert)是集合键的底层实现之一,当一个集合只包含整数值元素,并且这个集合的元素数量不多时,Redis就会使用整数集合作为集合键的底层实现.举个栗子,如果我们创建一个只包含五个 ...

  10. Java多线程并发技术

    Java多线程并发技术 参考文献: http://blog.csdn.net/aboy123/article/details/38307539 http://blog.csdn.net/ghsau/a ...