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. vmware完整克隆(linux)

    vmware中的完整克隆是基于指定的虚拟机克隆出相同的一份出来,不必再安装 但是我们要保证三个地方不能一样,一个是主机名称(hostname),一个是虚拟网卡设备mac地址,还有一个是ip地址 所以我 ...

  2. 16进制字符串转QByteArray,char转16进制字符串

    直接上代码,看代码你们就懂了 1.16进制QString转QByteArray QString str = "01 a5 1e 02"; QByteArray tmpBy; Str ...

  3. QT连接postgreSQL

    这是我之前项目遇到的问题,连接postgreSQL数据库,一直找不到引擎,最后终于找到 原因了,需要程序加载 1.安装postgresql客户端,2.需要配置postgresql客户端的bin和lib ...

  4. 【English】十五、“a”和“one”的区别是什么?

    一."a"和"one"的区别是什么 参考:“a”和“one”的区别是什么-百度知道 a和one的区别是什么?-作业帮 1.尽管a和one这两个在意义上有些相似, ...

  5. nginx地址代理(2)

    本章要讲的是服务器的代理: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器, 一对一代理:就一个服务器监听 server { listen       ...

  6. .Net Core 在Linux服务器下部署程序--(1). Windows 连接 Linux服务器

    下载Linux服务器连接软件,市面上有Putty,FinalShell等,我以FinalShell为例,下载地址为 :http://www.hostbuf.com/t/988.html,软件安装结束后 ...

  7. 使用docker快速搭建nginx+php环境

    在朋友的强烈推荐下,走上了docker之路.经过了繁琐的docker环境安装,看了下镜像/容器的简单使用,开始进行nginx+php环境的搭建,本文记录一下在安装过程中的笔记. 原文地址:代码汇个人博 ...

  8. Ruby入坑指南

    1.1 简介 Ruby语言是由松本行弘(Matz)设计,是一门通用的.面向对象的.解释型语言. 1.2 Ruby?RUBY?ruby? 1.Ruby:用来表示编程的语言 2.ruby:是指一个计算机程 ...

  9. storm ui 网页一直出现提示loading summary

    在更换了一次storm的版本之后:访问 http://mini1:8080/index.html 来查看storm的运行情况,但是出现了网页一直出现提示loading summary,但是通过透明的弹 ...

  10. vue使用npm run build命令打包

    vue使用npm run build命令打包项目   当我们使用vue-cli脚手架完成一个项目的时候,下一步肯定会想要怎么把这个项目放到互联网上或者本地直接打开呢,我们在本地调试的时候只要命令行执行 ...