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