287. Find the Duplicate Number
环
Input: nums = [1,3,4,2,2]
Output: 2Input: nums = [3,1,3,4,2]
Output: 3Input: nums = [3,3,3,3,3]
Output: 3Last updated
环
Input: nums = [1,3,4,2,2]
Output: 2Input: nums = [3,1,3,4,2]
Output: 3Input: nums = [3,3,3,3,3]
Output: 3Last updated
0 → 1 → 3 → 2 → 4 → 2 → 4 → ...class Solution:
def findDuplicate(self, nums: List[int]) -> int:
slow, fast = 0, 0
while True:
slow = nums[slow]
fast = nums[nums[fast]]
if slow == fast:
break
slow = 0
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slow