There is an integer array which has the following features:

  • The numbers in adjacent positions are different.
  • A[0] < A[1] && A[A.length - 2] > A[A.length - 1].

We define a position P is a peek if:

A[P] > A[P-1] && A[P] > A[P+1]

Find a peak element in this array. Return the index of the peak.

Notice

The array may contains multiple peeks, find any of them.

Have you met this question in a real interview?

 
 
Example

Given [1, 2, 1, 3, 4, 5, 7, 6]

Return index 1 (which is number 2) or 6 (which is number 7)

Challenge

Time complexity O(logN)

LeetCode上的原题,请参见我之前的博客Find Peak Element

解法一:

class Solution {
public:
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> A) {
int left = , right = A.size() - ;
while (left < right) {
int mid = left + (right - left) / ;
if (A[mid] < A[mid + ]) left = mid + ;
else right = mid;
}
return right;
}
};

解法二:

class Solution {
public:
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> A) {
for (int i = ; i < A.size(); ++i) {
if (A[i] < A[i - ]) return i - ;
}
return A.size() - ;
}
};

[LintCode] Find Peak Element 求数组的峰值的更多相关文章

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

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

  2. Lintcode: Find Peak Element

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

  3. LintCode "Find Peak Element II"

    Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...

  4. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...

  5. [Swift]LeetCode162. 寻找峰值 | Find Peak Element

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

  6. Leetcode之二分法专题-162. 寻找峰值(Find Peak Element)

    Leetcode之二分法专题-162. 寻找峰值(Find Peak Element) 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1] ...

  7. [LeetCode] 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) 29

    162. 寻找峰值 162. Find Peak Element 题目描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元 ...

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

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

随机推荐

  1. apk逆向 - smali动态调试

    author: Dlive date: 2016/10/6 0x00 前言 ​ 之前有人问过smali的动态调试方法,其实网上已经有很多文章讲这些内容,但是为了方便大家学习,我还是写一下让大家少走点坑 ...

  2. 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。

    第一步: windows 8系统,运行aspx页面出现上述错误信息,解决办法如下: iis7 :控制面板->打开或关闭windows功能->Internet信息服务->万维网服务-& ...

  3. 【转】Caffe初试(七)其它常用层及参数

    本文讲解一些其它的常用层,包括:softmax-loss层,Inner Product层,accuracy层,reshape层和dropout层及它们的参数配置. 1.softmax-loss sof ...

  4. TFS API:二、TFS 代码查询工作项

    TFS API:二.TFS  代码查询工作项 首先我们需要认识TFS的两大获取服务对象的类. 他们分别为TfsConfigurationServer和TfsTeamProjectCollection, ...

  5. springMVC中 request请求数据绑定到Controller入参 过程剖析

    前言:Controller方法的参数类型可以是基本类型,也可以是封装后的普通Java类型.若这个普通Java类型没有声明任何注解,则意味着它的每一个属性都需要到Request中去查找对应的请求参数.众 ...

  6. centos安装tmux过程

    原文:https://gist.github.com/rothgar/cecfbd74597cc35a6018 # Install tmux on Centos release 6.5 # insta ...

  7. Excel文件下载功能

    HtmlTextWriter:能够帮助我们快速生成html语句. 下载功能实现代码 protected void Button1_Click(object sender, EventArgs e) { ...

  8. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  9. Datazen自定义地图

    Datazen的地图数据定义主要以ESRI的Shape文件格式为主,这是现如今被广泛使用的一种地图数据格式.在Datazen中,自定义地图需要提供如下两个地图数据定义文件: SHP文件提供地图的位置数 ...

  10. 优化Linux内核参数

    转自:http://www.centoscn.com/CentOS/config/2013/0804/992.html vim /etc/sysctl.conf 1.net.ipv4.tcp_max_ ...