当前位置:网站首页>leetcode:162. 寻找峰值【二分寻找峰值】
leetcode:162. 寻找峰值【二分寻找峰值】
2022-07-24 16:06:00 【白速龙王的回眸】

分析
二分寻找峰值,总是往高的那一侧走,总能走到峰值
然后注意是l <= r or l < r or l < r - 1
ac code
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
n = len(nums)
# 人往高处走,总能走到峰值
def get(i):
if i == -1 or i == n:
return -inf
return nums[i]
l, r, ans = 0, n - 1, -1
while l <= r:
mid = (l + r) // 2
if get(mid - 1) < get(mid) > get(mid + 1):
ans = mid
break
if get(mid) < get(mid + 1):
l = mid + 1
else:
r = mid - 1
return ans
总结
二分寻找峰值
边栏推荐
- Dynamics crm: sharing records for users and teams
- Database learning – select (multi table joint query) [easy to understand]
- Yolov6 trains its own data set
- G026-db-gs-ins-03 openeuler deployment opengauss (1 active and 2 standby or multiple standby)
- 2022 / 7 / 20 training record
- From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
- 【tf.keras】:版本从1.x升级到2.x遇到的一个问题:InvalidArgumentError: Cannot assign a device for operation embedding_
- yolov4 训练自己的数据集
- Leetcode 223. 矩形面积
- Urban safety series popular science - enter the high incidence period of drowning, if you know the common sense of life-saving
猜你喜欢

Dynamics 365: explain virtual entity from 0 to 1

Yolov6 trains its own data set

From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?

Urban safety series popular science - enter the high incidence period of drowning, if you know the common sense of life-saving

Mysql8 encountered the problem of stopping after the service was started

LaneATT

Parse string

iptables常用命令小清单

Yolo5face: why reinvent the face detector

LaneATT
随机推荐
Windows10 installation free redis
OpenMP入门
Use of ec200u-cn module
Some understanding of the rank sum of matrix and the rank of image
R语言可视化分面图、多变量分组嵌套多水平t检验、并指定参考水平、可视化多变量分组嵌套多水平分面箱图(faceting boxplot)并添加显著性水平、指定显著性参考水平
124 maximum path sum in binary tree
kubernetes GPU的困境和破局
iptables常用命令小清单
从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
Please talk about the financial products with a yield of more than 6%
Database learning – select (multi table joint query) [easy to understand]
Scala functions and their higher-order applications
【LeetCode】Day102-螺旋矩阵 II
REST风格
Windows10安装免安装版redis
Who is the "roll" king of the prefabricated vegetable track?
Yolo5face: why reinvent the face detector
R语言ggplot2可视化:ggplot2可视化基本散点图(scatter plot)、通过在theme_bw中指定参数base_size来改变轴标签的大小、并控制网格线和轴标签的大小
电话系统规则
Which is a good noise reduction Bluetooth headset? Ranking of the most cost-effective noise reduction Bluetooth headsets