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. LeetCode949-给定数字能组成的最大时间

    问题: 给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间. 最小的 24 小时制时间是 00:00,而最大的是 23:59.从 00:00 (午夜)开始算起,过得越久,时间 ...

  2. 20181229(守护进程,互斥锁,IPC,生产者和消费者模型)

    一.守护进程 守护进程:一个进程B守护另一个进程A,当被守护的进程A结束,进程B也就结束了.(不一定同生,但会同死) 两个特点: ①守护进程会在主进程代码执行结束后就终止 ②守护进程内无法再开启子进程 ...

  3. poj 2533Longest Ordered Subsequence

    Longest Ordered Subsequence Description A numeric sequence of ai is ordered if a1 < a2 < - < ...

  4. Linux命令之----tree

    命令简介 tree命令的中文意思为“树”,功能是以树形结构列出指定目录下的所有内容,包括所有文件.子目录及子目录里的目录和文件. 命令格式 tree [option] [directory]tree ...

  5. mysql-copy to tmp table

    今天数据后台数据反映有些迟缓后查看链接 processlist 发下好多 锁 和磁盘写入,   参考文章 : http://bbs.chinaunix.net/forum.php?mod=viewth ...

  6. java集群技术

    序言 越来越多的关键应用运行在J2EE(Java 2, Enterprise Edition)中,这些诸如银行系统和账单处理系统需要高的可用性(High Availability, HA),同时像Go ...

  7. python - 接口自动化测试 - RunTest - 测试用例加载执行/测试报告生成

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: run_test.py @ide: PyCharm Com ...

  8. Python-S9—Day85-ORM项目实战之forms组件以及Modelform补充、跨域请求及应用

    01 forms组件补充1 02 forms组件补充2 03 ModelForm回顾 04 浏览器的历史 05 jsonop实现跨域请求 06 jsonop实现跨域请求2 07 jsonop实现跨域请 ...

  9. python 学习分享-装饰器篇

    本篇内容为偷窃的~哈哈,借用一下,我就是放在自己这里好看. 引用地址:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 第一步: ...

  10. MapReduce 使用案例

    MapReduce 使用案例 MapReduce在面试过程中出现的频率还是挺高的,尤其是数据挖掘等岗位.通常面试官会出一个大数据题目,需要被试者根据题目设计基于MapReduce的算法来解答.我在一个 ...