A peak element is an element that is greater than its neighbors.

Given an input array nums, where nums[i] ≠ nums[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 nums[-1] = nums[n] = -∞.

Example 1:

Input: nums = [1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.

Example 2:

Input: nums = [1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where the peak element is 2,
  or index number 5 where the peak element is 6.

Note:

Your solution should be in logarithmic complexity.

-----------------------------------------------------------------------------------------------------------------------------------------

这个在leetcode上用O(n)算法可以过,但是在lintcode 上会超时。

可以用二分查找,二分查找有递归写法和迭代写法。

C++:迭代

class Solution {
public:
int findPeakElement(vector<int>& nums) {
if(nums.size() == ) return -;
int left = ,right = nums.size() - ;
int mid;
while(left < right){
mid = left + (right - left)/;
if(nums[mid] > nums[mid + ]) right = mid; //mid不会加1,因为它本身也有可能是“山顶”。
else left = mid + ; //会加1 的,因为nums[mid]肯定不是“山顶”,所以忽略它。
}
return left;
}
};

详情见官方题解:https://leetcode.com/articles/find-peak-element/

另一个迭代解法(在lintcode上):

class Solution {
public:
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> &A) {
// write your code here
if(A.size() == ) return -;
int l = ,r = A.size() - ;
int mid;
while(l < r){
mid = l + (r - l)/;
if(A[mid] < A[mid - ])
r = mid;
else if(A[mid] < A[mid + ])
l = mid + ;
else
return mid;
}
//mid = A[l] > A[r] ? l:r;
//return mid;
}
};

也有一个解法:

class Solution {
public:
int findPeakElement(vector<int>& nums) {
if(nums.size() == )
return -;
int left = ;
int right = nums.size() - ;
while(left + < right){
int mid = left + (right - left)/;
if(nums[mid] > nums[mid + ]) right = mid;
else if(nums[mid] < nums[mid + ]) left = mid;
//else return mid;
}
int mid = nums[left] > nums[right] ? left:right;
return mid;
}
};

(二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element的更多相关文章

  1. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  2. (二分查找 拓展) leetcode 69. Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  3. (二分查找 拓展) leetcode278. First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  4. lintcode 75 Find Peak Element

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

  5. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  6. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  7. ✡ leetcode 162. Find Peak Element --------- java

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

  8. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

  9. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

随机推荐

  1. 部署 apply plugin: 'realm-android'

    我在build.gradle中添加 apply plugin: 'realm-android' //依赖Realm数据库,插件化依赖  这个后,同步,清理,运行的时候报 应该在build.gradle ...

  2. 荣耀5.0以上手机(亲测有效)激活xposed框架的经验

    对于喜欢搞机的朋友而言,大多时候会使用到xposed框架及其种类繁多功能强悍的模块,对于5.0以下的系统版本,只要手机能获得Root权限,安装和激活xposed框架是非常简便的,但随着系统版本的不断迭 ...

  3. ElasticSearch、Logstash、Kibana 搭建高效率日志管理系统

    ELK (ElasticSearch.LogStash以及Kibana)三者组合是一个非常强大的工具,这里我们来实现监控日志文件并且收到日志到ElasticSearch搜索引擎,利用Kibana可视化 ...

  4. node.js解析微信消息推送xml格式加密的消息

    之前写过一个解密json格式加密的,我以为xml的和json的差不多,是上上个星期五吧,我的同事也是在做微信公众号里面的消息推送解密,发现好像只能使用xml加密格式的发送到服务器,我们去年也做过企业微 ...

  5. SQL Server数据仓库的基础架构规划

    问题 SQL Server数据仓库具有自己的特征和行为属性,有别去其他.从这个意义上说,数据仓库基础架构规划需要与标准SQL Server OLTP数据库系统的规划不同.在本文中,我们将介绍在计划数据 ...

  6. JavaScript(四)变量

    变量的声明 在JavaScript程序中,使用一个变量之前应当使用关键字var进行声明,如下所示:var num;var sum; 也可以写成var num,sum,avg;如果只是声明变量而没有给变 ...

  7. saiku环境搭建

    说明:搭建saiku环境,BI展示工具. 环境说明: os:windows7 jdk:jdk1.6.0_43 tomcat:apache-tomcat-7.0.62 saiku:saiku-ui-2. ...

  8. Webdriver之API详解(3)

    前言 前两篇API链接 https://www.cnblogs.com/linuxchao/p/linuxchao-selenium-apione.html https://www.cnblogs.c ...

  9. jeecg入门操作—树型表单开发

    树表类型表单 表单创建,基础配置如下: 1.设置表单类型为:单表; 2.是否树选择:是; 3.设置特殊字段:[树形表单父id][树开表单列] 结果测试

  10. day11(函数参数,函数对象,打散机制,函数嵌套调用)

    一,复习 # 什么是函数:具体特定功能的代码块 - 特定功能代码块作为一个整体,并给该整体命名,就是函数 # 函数的优点: # 1.减少代码的冗余 # 2.结构清晰,可读性强 # 3.具有复用性,开发 ...