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.

Note:

Your solution should be in logarithmic complexity.


题目标签:Array, Binary Search

  题目给了我们一个array,让我们找到任意一个峰值,而且肯定有至少一个峰值存在。既然题目说了要用O(logn),那么就要用二分法了。
  利用二分法是为了,比较中间的值,和其他值,来判定,要去左半边还是右半边继续搜索。那么如何来判断峰值在哪里呢?
  这里有4种情况来判断峰值在哪儿?
  1. nums[mid-1] < nums[mid] > nums[mid+1]  这种情况是中间最高,两边都低,那么中间的已经是一个峰值了,直接返回index 就可以;
  2. nums[mid-1] < nums[mid] < nums[mid+1]  这种是递增趋势,那么峰值一定会在mid 的右边,缩小到右半边;
  3. nums[mid-1] > nums[mid] > nums[mid+1]  这种是递减趋势,那么峰值一定会在mid 的左边,缩小到左半边;
  4. nums[mid-1] > nums[mid] < nums[mid+1]  这种情况是中间低,两边都高,那么两边都会有峰值,去任意一边搜索。
 
  直到只有两个数字或者一个数字的时候,返回大的那一个数字的index就可以。
 
 

Java Solution:

Runtime beats 31.71%

完成日期:08/31/2017

关键词:Array, Binary search,

关键点:4种可能性来判断峰值的位置

 class Solution
{
public int findPeakElement(int[] nums)
{
int left = 0;
int right = nums.length -1; while(left < right - 1)
{
int mid = left + (right - left) / 2;
// if find peak number
if(nums[mid-1] < nums[mid] && nums[mid] > nums[mid+1])
return mid;
else if(nums[mid-1] < nums[mid] && nums[mid] < nums[mid+1])
{ // if find ascending three numbers, the peak number must be in [mid+1, n-1]
left = mid + 1;
}
else if(nums[mid-1] > nums[mid] && nums[mid] > nums[mid+1])
{ // if find descending three numbers, the peak number must be in [0, mid-1]
right = mid - 1;
}
else if(nums[mid-1] > nums[mid] && nums[mid] < nums[mid+1])
{ // if find the mid number is smaller than both neighbors, meaning both sides have peak number
right = mid - 1;
}
} return nums[left] > nums[right]? left:right;
}
}

参考资料:

https://discuss.leetcode.com/topic/5848/o-logn-solution-javacode/14

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 162. Find Peak Element (找到峰值)的更多相关文章

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

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

  2. LeetCode 162 Find Peak Element

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

  3. ✡ 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] ≠ ...

  4. (二分查找 拓展) 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 ...

  5. leetcode 162 Find Peak Element(二分法)

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

  6. 162 Find Peak Element 寻找峰值

    峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...

  7. Java for 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(M)(P)

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

  9. LeetCode——162. Find Peak Element

    一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...

随机推荐

  1. Thinkphp3.2结合phpqrcode生成二维码(含Logo的二维码),附案例

    首先,下载phpqrcode,将其解压到项目ThinkPHP\Library\Vendor目录下.Index_index.html(模板可自行配置) <form action="{:U ...

  2. 《Head First Java》读书笔记(1) - Java语言基础

    <Head First Java>(点击查看详情) 1.写在前面的话 这本书的知识点说实话感觉有点散乱,但是贵在其将文字转换成了生动和更容易接受的图片,大量的比喻让人感受到了知识点的有趣之 ...

  3. JavaScript基础回顾

    1, NaN 不是数字  Infinity 无穷大 alert(parseInt("dd")); //NaN alert(1/0); //Infinity 2, 逻辑或 || ,返 ...

  4. oracle 表查询(二)

    1.使用逻辑操作符号问题:查询工资高于500或者是岗位为manager的雇员,同时还要满足他们的姓名首字母为大写的J?select * from emp where (sal > 500 or ...

  5. JavaScript案例开发之扑克游戏

    随着时代的发展,知识也在日益更新,但是基础知识永远不会过时,它是新时代的基石,更是我们进一步学习的保障,下面带着大家用JavaScript开发一款真正的扑克游戏,和大家一起分享,希望你们能够喜欢:闲话 ...

  6. include 和require的区别

    相同点:include和require 都能把另外一个文件包含到当前文件中. 不同点:1.使用include时,当包含的文件不存在时,系统会报出警告级别的错误,程序会继续往下执行.   使用requi ...

  7. Java伪代码描述《大道至简》第一章

    第一节 Begin //原始需求 惩山北之塞,出入之迂 //项目沟通的基本方式 聚室而谋曰 //项目目标 毕力平险,指通豫南,达于汉阴 //技术方案 扣石垦壤,箕畚运于渤海之尾 //技术人员和工程管理 ...

  8. Python操作excel表格

    用Python操作Excel在工作中还是挺常用的,因为毕竟不懂Excel是一个用户庞大的数据管理软件 注:本篇代码在Python3环境下运行 首先导入两个模块xlrd和xlwt,xlrd用来读取Exc ...

  9. MongoDB学习教程(3)-常用命令

    1.MongoDB 条件操作符 描述 条件操作符用于比较两个表达式并从mongoDB集合中获取数据. 在本章节中,我们将讨论如何在MongoDB中使用条件操作符. MongoDB中条件操作符有: (& ...

  10. Java 中静态方法 实例方法 具体方法区别与联系

    在查阅JDK文档时,经常会看到某个类的方法汇总,一般会以如下的格式列出来: 这几个标签对应的方法类型分别是什么意思呢? 1.   Static Method,静态方法,可以在不创建类实例的情况下,访问 ...