-
Notifications
You must be signed in to change notification settings - Fork 0
/
findDuplicates.py
32 lines (27 loc) · 972 Bytes
/
findDuplicates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# LeetCode 287
# Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
# There is only one repeated number in nums (can repeat multiple times), return this repeated number.
# You must solve the problem without modifying the array nums and uses only constant extra space.
def findDuplicates(nums):
# Find the intersection point of the two runners.
tortoise = hare = nums[0]
print('start: ' + str(tortoise))
print('phrase 1 =============')
while True:
tortoise = nums[tortoise]
hare = nums[nums[hare]]
print('tortoise: ' + str(tortoise))
print('hare: ' + str(hare))
if tortoise == hare:
break
# Find the "entrance" to the cycle.
tortoise = nums[0]
print('phrase 2 =============')
while tortoise != hare:
tortoise = nums[tortoise]
hare = nums[hare]
print('tortoise: ' + str(tortoise))
print('hare: ' + str(hare))
return hare
t1 = [2, 9, 1, 5, 3, 6, 8, 9, 7, 9]
findDuplicates(t1)