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. Eclipse rap 富客户端开发总结(7) : 如何修改rap的样式

    1. Rap样式原理  Rap的界面样式目前是以css来配置的,程序启动后加载相应的css配置文件再对组件进行样式设置,界面上的所有组件 Label button composit等的样式最开始都是通 ...

  2. python之进程----Queue

    一.Queue是通过multiprocessing使用 from multiprocessing import Process,Queue import time import random impo ...

  3. u盘分区装机版

    第一步:制作启动U盘前的软硬件准备 .准备一个最好大于2G的U盘,并先备份好里面的数据,防止接下来需要清空U盘的数据而出现丢失重要文件: 2.下载U盘启动盘制作工具6.1软件,直接放在桌面,方便接下来 ...

  4. gephi安装后无法打开

    具体解决的方法是找到gephi.conf文件(在“gephi安装目录\etc”中)文件,添加下面的一行,指定jdkhome的路径. jdkhome="C:\Program Files (x8 ...

  5. 中国移动飞信WAP登陆分析及脚本

    中国移动飞信WAP网页版 http://f.10086.cn/im5/ 用WAP飞信登录并向好友发送信息,同时用wireshark抓包. 1.过滤POST表单提交数据包(wireshark规则: ht ...

  6. 接口测试——httpclient介绍与请求方式详解

    httpClient工具介绍 HTTP协议可能是现在lntemet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源.虽然在JDK的java.net包中已经 ...

  7. Rectangles hdu2461容斥定理

    Rectangles Time Limit: 5000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. java数据库编程之事务、视图、索引、备份、恢复

    第五章:事务.视图.索引.备份和恢复 5.1:事务 事务的概念:事务(transcation)是讲一系列数据操作捆绑成为一个整体进行统计管理. 如果某一事务执行成功了,则该事务进行操作的所有数据将会提 ...

  9. jQuery插件:Ajax将Json数据自动绑定到Form表单

    jQuery注册方法的两种常用方式: //jQuery静态方法注册 //调用方法$.a1() $.extend({ a1: function () { console.log("a1&quo ...

  10. Python实战之列表简单练习

    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__ ...