LeetCode287 寻找重复数(快慢指针寻找环入口)
作者:互联网
快慢指针寻找环入口
关于判定有环之后如果寻找环入口可参考 参考
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
slow, fast = nums[0], nums[nums[0]]
while slow != fast:
slow = nums[slow]
fast = nums[nums[fast]]
slow = 0
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slow
标签:LeetCode287,slow,nums,int,寻找,fast,入口,指针 来源: https://www.cnblogs.com/solvit/p/16436177.html