【LeetCode】162. Find Peak Element 解题报告(Python)

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/find-peak-element/description/

题目描述:

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.

click to show spoilers.

Note:
Your solution should be in logarithmic complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

题目大意

找到数组中的一个山峰节点位置,返回这个节点的位置。

解题方法

用两个mid,判断上坡还是下坡~二叉搜索,在这种题目中很常用~~

代码:

class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0, len(nums) - 1
while left < right:
mid1 = (left + right) / 2
mid2 = mid1 + 1
if nums[mid1] < nums[mid2]:
left = mid2
else:
right = mid1
return left

日期

2018 年 3 月 20 日 ————阳光明媚~

【LeetCode】162. Find Peak Element 解题报告(Python)的更多相关文章

  1. LeetCode: Find Peak Element 解题报告

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

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

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

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

  4. 【原创】leetCodeOj --- Find Peak Element 解题报告

    题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...

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

  7. LeetCode 162 Find Peak Element

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

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

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

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

随机推荐

  1. MYSQL(3)

    加载C盘下的目录 全表查询 查询部分字段 查询总数 条件过滤 and or 包含 范围检查 between and 否定结果not 匹配任意字符    like 以什么开始^   rlike 以什么结 ...

  2. Hadoop入门 完全分布式运行模式-准备

    目录 Hadoop运行环境 完全分布式运行模式(重点) scp secure copy 安全拷贝 1 hadoop102上的JDK文件推给103 2 hadoop103从102上拉取Hadoop文件 ...

  3. A Child's History of England.3

    So, Julius Caesar came sailing over to this Island of ours, with eighty vessels and twelve thousand ...

  4. java中的原子操作类AtomicInteger及其实现原理

    /** * 一,AtomicInteger 是如何实现原子操作的呢? * * 我们先来看一下getAndIncrement的源代码: * public final int getAndIncremen ...

  5. 【Services】【Web】【apr】安装apr

    1. 基础: 1.1 描述:apr全称Apache Portable Runtime,常用于与ssl相关的环境支持,比如openssl,httpd,nginx,tomcat 1.2 链接: 官方网站: ...

  6. TCP协议三步挥手与四步挥手

    关于TCP协议 TCP(Transmission Control Protocol, 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议.与之对应的是UDP(User Datagram ...

  7. 【C/C++】指针,传参,引用的一些个人理解。

    (以下均为个人理解) 函数访问的传参两种方式大致为: 值传递: 地址传递. 但是实际上可以都理解为,传进来的[形参]是主函数里的实参值的[一种复制]. 举个例子,哪怕我们将地址作为子函数的输入变量,形 ...

  8. 解决“该Jenkins实例似乎已离线”

    在jenkins/pluginManager/advanced最下面 把:https://updates.jenkins-ci.org/update-center.json 换成: 1.http:// ...

  9. ASP.NET管道模型简析

    我相信在第一次听到这个名词时,有的小伙伴会一脸懵,而且还有很多疑问,其实我在第一次接触这个概念时跟很多小伙伴一样一脸懵. 接下来我将以我自己的理解来讲述什么是管道模型. 什么是管道模型 首先有没有小伙 ...

  10. LeetCode 36. Valid Sudoku (Medium)

    题目 Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according ...