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. c 语言技巧

    位运算 & 位逻辑与 | 位逻辑或 ^ 位逻辑异或 - 位逻辑反 >> 右移 << 左移 通过对数据本身的01编码进行处理,速度稍微快于普通运算符 如,10 / 2 = ...

  2. IE console报错

    需要注意的是,使用console对象查看对象信息,在IE8浏览器下未打开开发人员工具(F12)的情况下 会报'console'未定义错误. 解决办法:1.打开开发人员调试工具(F12)        ...

  3. Python_常用模块

    一.内置模块 定义:其实模块简单说就是一堆代码实现某个功能,它们是已经写好的.py文件.只需要用import应用即可. 分类: 1. 自定义模块,就是自己写的.py文件为了实现某个功能. 2. 内置标 ...

  4. JZOJ 2136. 【GDKOI2004】汉诺塔

    2136. [GDKOI2004]汉诺塔 (Standard IO) Time Limits: 3000 ms  Memory Limits: 128000 KB  Detailed Limits   ...

  5. 14.VUE学习之-v-if v-else-if语法在网站注册中的实际应用讲解

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  6. linux硬件基础

    1. 服务器分类 机架式服务器(主要用这个). 刀片式服务器. 塔式服务器. 2. 机架式服务器 服务器的尺: U - 2U. 服务器核心之电源: 双电源 AB 路. 服务器核心之 CPU-计算 CP ...

  7. C++学习网站——www.cplusplus.com

    http://www.cplusplus.com/ 有各个函数.语法的实例代码,可以在线运行http://cpp.sh/不支持中文字符,不错.

  8. Flow Problem HDU - 3549

    Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...

  9. codeforce GYM 100741 A Queries

    A. Queries time limit per test:0.25 s memory limit per test:64 MB input:standard input output:standa ...

  10. Django templates(模板)

    为什么用templates? views.py视图函数是用来写Python代码的,HTML可以被直接硬编码在views.py之中.如下: import datetime def current_tim ...