当前位置:网站首页>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
总结
二分寻找峰值
边栏推荐
- SQL row to column, column to row
- 从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
- Arduino ide esp32 firmware installation and upgrade tutorial
- Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]
- Dedecms editor supports automatic pasting of word pictures
- REST风格
- With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!
- faster-rcnn 训练自己的数据集
- Mobile phone comparison redmi note8 and realm x2
- Class assignment (6) - 575. Word division (word)
猜你喜欢

电话系统规则

Talk about C pointer

torch_ How to use scatter. Scatter() in detail

Leetcode 220. duplicate element III exists

Leetcode 223. rectangular area

Arduino ide esp32 firmware installation and upgrade tutorial

About SQL data query statements

Rest style

Dynamics 365: how to get the threshold value of executemullerequest in batch requests

狗牙根植物介绍
随机推荐
图像label处理——json文件转化为png文件
Which is a good noise reduction Bluetooth headset? Ranking of the most cost-effective noise reduction Bluetooth headsets
2022/7/18 CF training
狗牙根植物介绍
聊聊C指针
Fast RCNN trains its own data set
C TCP client form application asynchronous receiving mode
微调LayoutLM v3进行票据数据的处理和内容识别
How to choose the appropriate data type for fields in MySQL?
Class assignment (6) - 575. Word division (word)
G026-db-gs-ins-03 openeuler deployment opengauss (1 active and 2 standby or multiple standby)
Mobile phone comparison redmi note8 and realm x2
Memcache cache application (lnmp+memcache)
Kubernetes version docking object storage
MySQL之知识点(十二)
Azure key vault (1) Introduction
Please talk about the financial products with a yield of more than 6%
20. Shell programming variables
iptables常用命令小清单
MySQL source code analysis -- data structure of index