当前位置:网站首页>leetcode:287. 寻找重复数【快慢指针板子】
leetcode:287. 寻找重复数【快慢指针板子】
2022-07-17 06:56:00 【白速龙王的回眸】

分析
n->nums[n]这样子
由于有至少一对ij使得nums[i] == nums[j]
所以肯定会有环的
环的入口就是重复的数字 nums[i]
快慢指针解决
第一次相遇后,重新从0走和当前位置走
再次相遇就是入口
ac code
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
# v in nums: v < len(nums)
# n -> f(n), 成环
# 找相遇点
slow, fast = nums[0], nums[nums[0]]
while slow != fast:
slow = nums[slow]
fast = nums[nums[fast]]
# 找入口
pre1 = slow
pre2 = 0
while pre1 != pre2:
pre1 = nums[pre1]
pre2 = nums[pre2]
return pre1
总结
快慢指针应用
边栏推荐
- 在线问题反馈模块实战(五):实现对通用字段内容自动填充功能
- C#对txt文件的读写操作
- redis集群
- the max_ iter was reached which means the coef_ did not converge “the coef_ did not converge“
- Facial key point detection CNN
- the max_iter was reached which means the coef_ did not converge “the coef_ did not converge“
- 深度学习之 7 深度前馈网络
- [C# Console]-C# 控制台类
- Xinlinx zynq7020, 7045 domestic replacement fmql45t900 national production arm core board + expansion board
- [MySQL] transaction: basic knowledge of transaction, implementation principle of MySQL transaction, detailed explanation of transaction log redolog & undo
猜你喜欢

Practice of online problem feedback module (V): realize the automatic filling function of general field content

Stm32f103c8t6 hardware IIC control 4-pin 0.96 inch OLED display

Ccf-csp "202206-2 - treasure hunt! Adventure!"

redis6新功能

类型详解·自定义类型·结构体初识

redis分布式锁

Facial key point detection CNN

《牛客刷题》sql错题集

“韭菜”是怎么把钱送给“镰刀”的? 2020-03-07

《痞子衡嵌入式半月刊》 第 58 期
随机推荐
分叉币的发展史及价值|ETH、BCH、BSV 2020-03-08
The website vulnerability repair service provider analyzes the ultra vires caused by controllable parameters
Code learning (deamnet) CVPR | adaptive consistency prior based deep network for image learning
Random forest of machine learning
Jira --- workflow call external api
数据库复习--数据库恢复技术
Double index mechanism of redis source code analysis
半导体材料技术
代码学习(DeamNet)CVPR | Adaptive Consistency Prior based Deep Network for Image Denoising
standard-version(发版与 Changelog 自动化)
演示集合注入
redis缓存雪崩、穿透、击穿
面试题:外边距折叠问题 (块级元素在普通文档流中的BUG)
“韭菜”是怎么把钱送给“镰刀”的? 2020-03-07
[MySQL] mvcc: correctly understand mvcc and its implementation principle
才意识到自己“奇葩”的360,会不会有些晚?
聊聊分布式锁
[C classes and objects] - Methods and class and object programming in C
RNN convolutional neural network
Jd.com's purchase intention forecast (III)