287. Find the Duplicate Number
Solution
-
Consider the array as a linked list.
-
The existence of the duplicate number shows the existence of the cycle in the linked list.
-
The entrance of the cycle is the duplicate number.
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
slow = nums[0]
fast = nums[nums[0]]
while slow != fast:
slow = nums[slow]
fast = nums[nums[fast]]
fast = 0
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slow
PREVIOUSCombination Sum