【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. Identity Server 4 从入门到落地(三)—— 创建Web客户端

    书接上回,我们已经搭建好了基于Identity Server 4的认证服务和管理应用(如果还没有搭建,参看本系列前两部分,相关代码可以从github下载:https://github.com/zhen ...

  3. jmeter非GUI(cmd命令行)模式的压测和输出测试报告

    1.非GUI模式的压测,和GUI有啥不同? 2.非GUI模式怎么搞? 大家打开jmeter的时候,都会看到这个界面: 注意看这句话: Don't use GUI mode for load testi ...

  4. 1 — 第一个springboot

    1.什么是springboot? 老规矩:百度百科一下 2.对springboot快速上手 1).第一种方式:通过官网来创建springboot项目 ---- 了解即可 这里面的创建方式不做过多说明, ...

  5. 华为AppTouch携手全球运营商,助力开发者出海

    内容来源:华为开发者大会2021 HMS Core 6 APP services技术论坛,主题演讲<华为AppTouch携手全球运营商,助力开发者出海>. 演讲嘉宾:华为消费者云服务App ...

  6. day04:Python学习笔记

    day04:Python学习笔记 1.算数运算符 1.算数运算符 print(10 / 3) #结果带小数 print(10 // 3) #结果取整数,不是四舍五入 print(10 % 3) #结果 ...

  7. oracle中的数组

    Oracle中的数组分为固定数组和可变数组. 一.固定数组固定数组:在定义的时候预定义了数组的大小,在初始化数组时如果超出这个大小,会提示ORA-06532:超出小标超出限制!语法:        T ...

  8. API接口设计之token、timestamp、sign 具体架构与实现(APP/小程序,传输安全)

    Java生鲜电商平台-API接口设计之token.timestamp.sign 具体设计与实现 说明:在实际的业务中,难免会跟第三方系统进行数据的交互与传递,那么如何保证数据在传输过程中的安全呢(防窃 ...

  9. Undefined symbols for architecture arm64:问题

    Undefined symbols for architecture arm64: "_sqlite3_prepare_v2", referenced from: +[HMJSch ...

  10. Linux服务器---drupal

    Drupal Drupal为用户提供各种工具来管理网站,它可以帮助用户入门,建立自己的网站 1.下载drupal软件(https://www.drupal.org/project/drupal/rel ...