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.

思路就是要么在上升(l = mid), 要么在下降(r = mid), 要么在谷底或者顶点(l = mid or r = mid) 都可以.最后判断A[l] < A[r] , return r else l

04/14/2019 update:

分四种情况:   nums[mid - 1] ? nums[mid]  ? nums[mid + 1]

1)   < > , 峰值,直接返回

2)< <, 上升,取右边,因为nums[-1] = 负无穷

3)> >, 下降,取左边

4)> < , 低谷, 取右边, 可以将2与4合并

Code

class Solution:
def findPeakElement(self, nums):
l, r = 0, len(nums)-1 # 可以是0 or len(nums) -1
while l + 1 < r:
mid = l + (r-l)//2
if nums[mid] < nums[mid - 1]:
r = mid
elif nums[mid] < nums[mid + 1]:
l = mid
else:
l = mid # or r = mid
if nums[l] < nums[r]:
return r
return l

2) update

class Solution:
def findPeakElement(self, nums):
l,r = 0, len(nums) - 1
while l + 1 < r:
mid = l + (r - l)//2
if nums[mid - 1] < nums[mid] > nums[mid + 1]:
return mid
elif nums[mid - 1] > nums[mid] > nums[mid + 1]:
r = mid
else:
l = mid
return r if nums[r] > nums[l] else l

[LeetCode] 162. Find Peak Element_Medium tag: Binary Search的更多相关文章

  1. [LeetCode] 278. First Bad Version_Easy tag: Binary Search

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  2. [LeetCode] 69. Sqrt(x)_Easy tag: Binary Search

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  3. [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  4. [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  5. [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  6. [LeetCode] 704. Binary Search_Easy tag: Binary Search

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  7. [LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  8. [LeetCode] 35. Search Insert Position_Easy tag: Binary Search

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

随机推荐

  1. jdbc --- javabean

    第一部分: javaBean 类  要和数据库表的字段一一对应 package com.ljs.bean; public class UserBean { private int id; privat ...

  2. 深入理解无穷级数和的定义(the sum of the series)

    Given an infinite sequence (a1, a2, a3, ...), a series is informally the form of adding all those te ...

  3. es6 数组实例的 find() 和 findIndex()

    数组实例的find方法,用于找出第一个符合条件的数组成员.它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员.如果没有符合条件的成员,则返回u ...

  4. ios APP改名推送名字还是旧的

    重启手机就行了 https://community.jiguang.cn/t/ios-app/14759

  5. C# decimal指定精度

    最近做一个项目.遇到了decimal 如何指定精度的问题 一般的指定参数    param = new SqlParameter(ParamName, DbType); 但decimal就不能只通过构 ...

  6. nodejs prefix(全局)和cache(缓存)windows下设置

    引:在安装完nodejs后,通过npm下载全局模块默认安装到{%USERDATA%}C:\Users\username\AppData\下的Roaming\npm下,这当然是不太对的默认. 1,安装L ...

  7. JavaScript字符串String

    JavaScript中String类型用于表示由零个或者多个16位Unicode字符组成的字符序列即字符串:同时字符串可以用单引号或双引号表示. 下面是一些特殊的字面量: 字面量 含义\n 换行\t ...

  8. es定制排序搜索结果

    GET /company/employee/_search { "query": { "constant_score": { "filter" ...

  9. Hibernate 和 MyBatis 的区别

    Hibernate 和 MyBatis 的增.删.查.改,对于业务逻辑层来说大同小异,对于映射层而言 Hibernate 的配置不需要接口和 SQL,相反 MyBatis 是需要的.对于 Hibern ...

  10. 图解HTTP笔记

    http 0.9  1990年  http 1.0  1996年 Tcp/Ip分层模型 应用层: 如Ftp Http DNS. 传输层: 如 TCP UDP .将报文分成报文段.“传输”指的是端对端( ...