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

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

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

click to show spoilers.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.


【题目分析】

在一个数组中任意两个相邻元素是不相同的,找到这样的元素,它比它的邻居节点都要大。如果存在这样的元素,返回其中任意一个即可。


【解体思路】

方法1:

遍历数组,

  • 如果是最左边的元素,并且它比右邻居大,那它就是peak elmemnt。
  • 如果是最右边的元素,并且它比左邻居大,也是peak element。
  • 否则,如果它比左右邻居都大,那么它也是peak element。

代码如下:

 class Solution
{
public:
int findPeakElement(vector<int>& nums)
{
if (nums.size() == 1)
{
return 0;
} for (int i = 0; i < nums.size(); i++)
{
if ((i == 0 && nums[i] > nums[i + 1])
|| (i == nums.size() - 1 && nums[i] > nums[i - 1]))
{
return i;
} if (0 < i && i < nums.size() - 1
&& nums[i] > nums[i - 1] && nums[i] > nums[i + 1])
{
return i;
}
} return -1;
}
};

方法2:

上面的算法用到了很多次比较,下面给出一段更简单的代码:

 class Solution
{
public:
int findPeakElement(vector<int>& nums)
{
// 对于nums[0]:题目规定它大于左邻居(不存在),因此只要它比右邻居大,那它就是peak element。
// 如果不是,那说明它不比右邻居大。而题目规定相邻节点不相等,因此它必然小于右邻居。
//
// 对于nums[1]:由上面的分析可知:左邻居小于当前节点。因此只要它比右邻居大,那就是peak element了。
// 如果也不是,那说明它也比右邻居小。
// ...
// 对于nums[sz - 2]:如果之前都没有返回。由上面规律可知:它比左邻居大。
// 而按照题目规定,它又一定大于右邻居(不存在)。因此它一定是peak element。
for (int i = 0; i < nums.size() - 1; i++)
{
if (nums[i] > nums[i + 1])
{
return i;
}
}
return nums.size() - 1;
}
};

上面两个解法都能够被LeetCode接受,但是它们的时间复杂度都是是O(n)。而题目中要求是:O(log n)


方法3:

由上面的分析,我们可以得出这样一个规律:

只要数组满足下面的形式:

[ 小,大,... 未知 ...,大,小 ]

那么其中一定能找到peak elememnt。

尝试用二分法处理:

取数组的中间值mid

  • 如果mid的值比mid+1的值小

    那么从mid开始的数组右半部分将满足:[小,大,...,大,小]的形式,所以其中一定有peak element。

    又因为mid肯定不是peak element,所以可以进一步把范围缩小为[mid+1, 数组末尾]

  • 如果mid的值比mid+1的值大

    那么从数组的开始位置mid+1位置,也将构成[小,大,...,大,小]的形式。

    mid+1显然也不是peak element。所以可进一步把范围缩小到[数组开始,mid]

代码如下:

 public class Solution {
public int findPeakElement(int[] nums) {
int left = 0, right = nums.length-1;
while(left < right){
int mid = (right - left)/2 + left;
if(nums[mid] > nums[mid+1])
right = mid;
else
left = mid + 1;
}
return left;
}
}

LeetCode OJ 162. Find Peak Element的更多相关文章

  1. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  2. 【LeetCode】162. Find Peak Element (3 solutions)

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  3. LeetCode OJ:Find Peak Element(寻找峰值元素)

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

  4. 【刷题-LeetCode】162 Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  5. leetcode 日记 162. Find Peak Element java python

    根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号.并且要求时间复杂度为logn 分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历.又因为只需要找到其中的一个 ...

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

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

  7. (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element

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

  8. [LeetCode] 162. Find Peak Element 查找峰值元素

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

  9. LeetCode 162 Find Peak Element

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

随机推荐

  1. 面试题-Java基础-Applet部分

    java applet是能够被包含在HTML页面中并且能被启用了java的客户端浏览器执行的程序.Applet主要用来创建动态交互的web应用程序.

  2. 取汉子拼音首字母的C#和VB.Net方法

    转载http://blog.fwhyy.com/2012/03/take-the-first-initials-method-of-csharp-and-vbnet/

  3. re模块 | Python 3.5

    https://docs.python.org/3/library/re.html http://www.cnblogs.com/PythonHome/archive/2011/11/19/22554 ...

  4. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  5. redis写shell与ssh免密码登陆

     redis-cli参数:-h :指定要连接的主机IP或域名-p :指定连接的端口-a :指定密码-r :执行指定的命令-n :数据库名-x :将最后一个参数输出为value redis写shell- ...

  6. 解决 bootstrap 在IE8下的兼容问题

    <meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="v ...

  7. 面试题-Java基础-集合和数组

    1.Java集合类框架的基本接口有哪些? 集合类接口指定了一组叫做元素的对象.集合类接口的每一种具体的实现类都可以选择以它自己的方式对元素进行保存和排序.有的集合类允许重复的键,有些不允许.Java集 ...

  8. ResScope (软件资源分析)V1.94 绿色版

    软件名称:ResScope (软件资源分析)V1.94 绿色版软件类别:国产软件运行环境:Windows软件语言:简体中文授权方式:免费版软件大小:1.47 MB软件等级:整理时间:2015-01-0 ...

  9. Leetcode - 186 Reverse Words in a String II

    题目: Given an input string, reverse the string word by word. A word is defined as a sequence of non-s ...

  10. 微信小程序开发详解——小程序,大颠覆!

    微信小程序开发 联系 苏念 188.1414.7927  微信小程序系统开发 微信新功能开发 小程序开发 小程序怎么开发 app小程序开发 简化小程序开发 微信小程序定制 小程序制作 开发微信小程序  ...