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. D - Replace To Make Regular Bracket Sequence

    You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...

  2. API(一)之Serialization

    virtualenv is a tool to create isolated Python environments. 建立一个新的环境 Before we do anything else we' ...

  3. tsm 切记

    切记不可删除节点,如果删除下面带的数据也会删除

  4. WordOperate

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  5. bootstrap-switch 使用

    网址:http://www.bootcss.com/p/bootstrap-switch/ 界面设置不调用方法没成功,事件也不起作用不知道是jquery版本原因还是什么原因!,下面亲测试可以使用 $( ...

  6. 7.26-STOIRegularMatch-08-#14

    A-3 SRM 08 描述 给一个 01 串设为其 S,询问是否存在只出现两次的 01 串 T. 这里的出现定义为存在一串下标 ,满足  且 . 输入格式 一行,一个 01 串 输出格式 一行,字母 ...

  7. *** FATAL ERROR L250: CODE SIZE LIMIT IN RESTRICTED VERSION EXCEEDED

    *** FATAL ERROR L250: CODE SIZE LIMIT IN RESTRICTED VERSION EXCEEDED 在软件已经执行破解仍然出现,是因为工程是破解前建立的,要先执行 ...

  8. strlen函数的汇编实现分析

    [原创][老刘谈算法001]这位运算玩的真溜—strlen函数的汇编实现分析-『密码算法』-看雪安全论坛 https://bbs.pediy.com/thread-229243.htm

  9. 如果是多个 c 代码的源码文件,编译方法如下: $ gcc test1.c test2.c -o main.out $ ./main.out test1.c 与 test2.c 是两个源代码文件。

    如果是多个 c 代码的源码文件,编译方法如下: $ gcc test1.c test2.c -o main.out $ ./main.out test1.c 与 test2.c 是两个源代码文件.

  10. ORACLE之PACKAGE-包、存储过程、函数

    1,简单的包. 创建包规范: create or replace package pack_test1 is -- 定义过程1 procedure p_test1(p_1 in varchar2); ...