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. Day-14: 常用的内建模块

    collections包含对tuple.list.dict等派生出新功能 namedtuple用来为tuple类型派生出一个新名字的tuple类,并提供用属性引出的功能. >>> f ...

  2. Python的自学之路:Python基础(一)

    声明:我写博客不是为了什么,只是为了记录自己的学习状态,学过的知识点!方便以后进行好的复习!python小白,勿喷 python环境的搭建,在这里就不细说了,这里有我的链接,可以参考一下:https: ...

  3. python 浅析对return的理解

    最近很忙,但是还是很认真的学习python这个东西,不是出于什么目的,只是单纯的喜欢罢了.最近学习的东西比较简单,但是也遇到了一些问题,就是比较迷惑人的问题,今天小编就在这里讲讲自己的对return的 ...

  4. 如何在Oracle官网下载历史版本JDK

    打开Oracle官网,准备下载java JDK(下载时需要使用注册用户登陆,可以免费注册) 官网地址:http://www.oracle.com/ 2 鼠标悬停Downloads,会出现相关内容,如下 ...

  5. String类的源码分析

    之前面试的时候被问到有没有看过String类的源码,楼主当时就慌了,回来赶紧补一课. 1.构造器(构造方法) String类提供了很多不同的构造器,分别对应了不同的字符串初始化方法,此处从源码中摘录如 ...

  6. 第一个asp.net MVC5+ExtJS6入门案例项目

    最近在学习asp.net MVC,结合前段时间学习的ExtJS,做了一个入门示例.不过还有一个json日期显示的问题没有解决. [思路] 1.先搭建一个asp.net MVC项目. 2.将MVC项目的 ...

  7. 【Linux笔记(001) 】-- centos7 系统目录结构与文件

    一.目录结构与用途: /boot:系统引导文件.内核 /bin:用户的基本命令 /dev:设备文件 /etc:配置文件 /home:用户目录 /root:root用户目录 /sbin:管理类的基本命令 ...

  8. mybatis 架构

    官网地址:http://code.google.com/p/mybatis/ 版本:mybatis 3.2.3 生成工具:mybatis-generator-core-1.3.2-bundle.zip ...

  9. 关于 LindedList 我想说

    LinkedList 的一些认识: 继承于AbstractSequentialList的双向链表,可以被当作堆栈.队列或双端队列进行操作 有序,非线程安全的双向链表,默认使用尾部插入法 适用于频繁新增 ...

  10. 每周分享之 二 http协议(1)

    本次分享http协议,共分为三部分,这是第一部分,主要讲解http的发展历程,各个版本,以及各个版本的特点. 一:http/0.9 最早版本是1991年发布的0.9版.该版本极其简单,只有一个命令GE ...