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:

  1. 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

解法一:

  1. class Solution {
  2. public:
  3. /**
  4. * @param A: An integers array.
  5. * @return: return any of peek positions.
  6. */
  7. int findPeak(vector<int> A) {
  8. int left = , right = A.size() - ;
  9. while (left < right) {
  10. int mid = left + (right - left) / ;
  11. if (A[mid] < A[mid + ]) left = mid + ;
  12. else right = mid;
  13. }
  14. return right;
  15. }
  16. };

解法二:

  1. class Solution {
  2. public:
  3. /**
  4. * @param A: An integers array.
  5. * @return: return any of peek positions.
  6. */
  7. int findPeak(vector<int> A) {
  8. for (int i = ; i < A.size(); ++i) {
  9. if (A[i] < A[i - ]) return i - ;
  10. }
  11. return A.size() - ;
  12. }
  13. };

[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. 关于imageOrientation

    用相机拍出来的照片都含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息.如果我们忽略orientation信心,而直接对照片进行想速处 ...

  2. C++ Bitstream类

    从raknet上剥下来的 比较适用于前后端通讯,可以对BitStream进行二次封装,方便使用. BitStream.h: #ifndef __BITSTREAM_H #define __BITSTR ...

  3. set和map的简单用法

    .set(集合)map(映射)都属于关联类容器 都支持查询一个元素是否存在并能够有效地获取元素. set集合的元素总是从小到大排列,set集合通过二分查找树实现.它具备以下两个特点: ①:独一无二的元 ...

  4. 多个html怎么导入相同的头部导航

    1. iframe 包含法.页头和页尾分别做成一个页面,然后通过iframe嵌入到调用的页面.这种方法在页头页尾高度固定的时候比较适用,因为当页头页尾高度不固定时,需要iframe根据页面内容自适应高 ...

  5. php 封装 知识点

    类由众多对象抽象出来的对象由类实例化出来的 成员变量成员方法成员属性 访问修饰符public 公有的protected 受保护的private 私有的 构造函数1.写法特殊2.执行时间特殊 面向对象的 ...

  6. Android版本与api Level

    Platform Version API Level VERSION_CODE Notes Android 4.4 19 KITKAT Platform Highlights Android 4.3 ...

  7. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  8. ****LINUX命令(含GIT命令)个人总结

    参考文章: 每日一个linux命令 http://www.cnblogs.com/peida/tag/%E6%AF%8F%E6%97%A5%E4%B8%80linux%E5%91%BD%E4%BB%A ...

  9. sqlserver 查找某个字段在哪张表里

    select [name] from [库名].[dbo].sysobjects where id in(select id from [库名].[dbo].syscolumns Where name ...

  10. ExtJS 中类的选项 - config

    首先看一个例子,我们在ExtJS中定义一个Window对象,代码如下: var win = Ext.create("Ext.window.Window", { title: '示例 ...