【LeetCode】162. Find Peak Element 解题报告(Python)
【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)的更多相关文章
- LeetCode: Find Peak Element 解题报告
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 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] ≠ ...
- 【原创】leetCodeOj --- Find Peak Element 解题报告
题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- (二分查找 拓展) 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 ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- ✡ 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] ≠ ...
- leetcode 162 Find Peak Element(二分法)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- rabbit mq的一个实例,异步功能
简单的使用场景:消息队列的场景有:解耦,异步,削峰. 此例用的场景,异步 有时候会有请求消耗时间过长,不能老让用户等待返回结果,可以用消息队列来做异步实现,之前用过workmain等类似的异步,但不如 ...
- javaSE基础知识(走向编程的门口)— 更新完毕
前言:玩儿编程最重要的一点:不要怕麻烦,感觉是在浪费时间: 能动手绝不哔哔:只要脑袋不傻,编程都是"一看就会,一练就废",开始学的时候,就算再基础的东西都建议手敲一遍 要有囫囵吞枣 ...
- linux shell中的条件判断语句
http://bbs.chinaunix.net/thread-396805-1-1.html shell 判断语句 流程控制 "if" 表达式 如果条件为真则执行then后面的部 ...
- Linux基础命令---alias别名
alias Alias不带参数或使用-p选项在标准输出上以"name=value"的形式打印别名列表.当提供参数时,为其值给定的每个名称定义一个别名.值中的尾随空格将导致在扩展别名 ...
- 使用springboot配置和注入数据源属性的方法和步骤
/** 1.书写一个名为resources/application.properties的属性文件---->书写一个配置属性类,类名为: **/ 文件:application.propertie ...
- request.getRequestDispatcher()和response.sendRedirect()区别
一.request.getRequestDispatcher().forward(request,response): 1.属于转发,也是服务器跳转,相当于方法调用,在执行当前文件的过程中转向执行目标 ...
- libev I/O事件
libev是来实现reactor模式,主要包含三大部分: 1. watcher:watcher是Reactor中的Event Handler. 作用:1)向事件循环提供了统一的调用接口(按类型区分) ...
- python使用gitlab-api
目录 一.简介 二.示例 讲解 配置文件方式存储token 其它返回 三.其它操作 一.简介 公司使用gitlab 来托管代码,日常代码merge request以及其他管理是交给测试,鉴于操作需经常 ...
- time_formatter(uaf)
拿到题目先例行检查 然后进入主函数查看程序流程, 进入函数 这些字符串对我们选择1的输入进行了限制 在输入里面,可以看到strdup这个关键性的函数 调用了malloc这个函数 在选择四里面,可以看到 ...
- 修复 Edge 浏览器 1Password 插件 Ctrl+Shift+X 弹出快捷键失效
解决方式 在 Edge 浏览器右上角 1Password插件图标上右键,选择设置: 在打开的 1Password 设置页面中,找到快捷键设置环节,默认使用快捷键打开后面为空,点击"在扩展也上 ...