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. vue 数据改变但是视图没更新

    在使用过程中会出现数据改变但是视图没有更新的情况(类型数组或者对象),这里我们就需要用到 $set 如果是对象类型: this.$set(this.userInfo, 'name', 'gionlee ...

  2. AndroidStudio使用问题记录

    问题: Gradle sync failed: Connection timed out: connect Consult IDE log for more details (Help | Show ...

  3. Java:配置环境(Mac)——Tomcat

    1.官网下载 2.把下载的文档解压,放到合适的路径下. 3.打开eclipse 4.在Apache文件夹下选择Tomcat的对应版本 5.选择刚才下载的文件 6.可以右键Start了

  4. 结对编程项目总结 by:陈宏伟&刘益

    结对编程项目在欢快的国庆假期中也顺利结束了.从最初拿到结对编程项目的思考,再到一步一步实现,中间经历了一个漫长的过程.在我和队友的多次协商下,最终我们还是选择使用基于python来实现这一次结对编程项 ...

  5. IDEA启动tomcat乱码

    1.找到IDEA安装目录 2.找到2个文件 3.编辑,在最后一行加入 -Dfile.encoding=UTF-8 4.修改IDEA里tomcat内得编码 5.修改IDEA中tomcat中,startu ...

  6. MySQL 关于性能的参数配置梳理

    以下List是我们常见的MySQL参数配置,这个参数对提高实例的性能大有裨益. 其中 建议设置值,仅供参考,需要根据自己的业务场景和硬件资源仔细推敲. 参数 设置说明 建议设置值 lower_case ...

  7. SQL MIN() 函数

    MIN() 函数 MIN 函数返回一列中的最小值.NULL 值不包括在计算中. SQL MIN() 语法 SELECT MIN(column_name) FROM table_name 注释:MIN ...

  8. java中String的final类原因

    public final class String implements java.io.Serializable, Comparable<String>, CharSequence { ...

  9. Mysql数据库引擎介绍--转载

    引用博文链接:https:/www.cnblogs.com/zhangjinghe/p/7599988.html MYSQL数据库引擎区别详解 数据库引擎介绍 MySQL数据库引擎取决于MySQL在安 ...

  10. 身份认证功能chiro的使用

    package com.cun;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.*;import org.apa ...