Skip to content

Commit

Permalink
Update find-the-duplicate-number.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Sep 28, 2015
1 parent 70077a7 commit cbe0c3d
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions Python/find-the-duplicate-number.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@ def findDuplicate(self, nums):
:rtype: int
"""
# Treat each (key, value - 1) pair of the array as the (pointer, next) node of the linked list,
# thus the duplicated number will be the begin of the cycle in the linked list.
# Besides, there is always a cycle in the linked list which
# starts from the last element of the array.
slow = nums[len(nums) - 1]
fast = nums[nums[len(nums) - 1] - 1]

# thus the duplicated number will be the begin of the cycle in the linked list.:
slow = nums[0]
fast = nums[nums[0]]
while slow != fast:
slow = nums[slow - 1]
fast = nums[nums[fast - 1] - 1]

slow = nums[slow - 1]
fast = nums[len(nums) - 1]
slow = nums[slow]
fast = nums[nums[fast]]

fast = 0
while slow != fast:
slow = nums[slow - 1]
fast = nums[fast - 1]

slow = nums[slow]
fast = nums[fast]
return slow


Expand Down

0 comments on commit cbe0c3d

Please sign in to comment.