forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-the-duplicate-number.py
80 lines (72 loc) · 2.25 KB
/
find-the-duplicate-number.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Time: O(n)
# Space: O(1)
# Two pointers method, reference: http://keithschwarz.com/interesting/code/?dir=find-duplicate
class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# The "tortoise and hare" step. We start at the end of the nums and try
# to find an intersection point in the cycle.
slow = nums[len(nums) - 1]
fast = nums[nums[len(nums) - 1] - 1]
# Keep advancing 'slow' by one step and 'fast' by two steps until they
# meet inside the loop.
while slow != fast:
slow = nums[slow - 1]
fast = nums[nums[fast - 1] - 1]
# Start up another pointer from the end of the nums and march it forward
# until it hits the pointer inside the nums.
slow = nums[slow - 1]
finder = nums[len(nums) - 1]
while slow != finder:
slow = nums[slow - 1]
finder = nums[finder - 1]
# If the two hit, the intersection index is the duplicate element.
return slow
# Time: O(nlogn)
# Space: O(1)
# Binary search method.
class Solution2(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 1, len(nums) - 1
while left <= right:
mid = left + (right - left) / 2
# Get count of num <= mid.
count = 0
for num in nums:
if num <= mid:
count += 1
if count > mid:
right = mid - 1
else:
left = mid + 1
return left
# Time: O(n)
# Space: O(n)
class Solution3(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
duplicate = 0
# Mark the value as visited by negative.
for num in nums:
if nums[abs(num) - 1] > 0:
nums[abs(num) - 1] *= -1
else:
duplicate = abs(num)
break
# Rollback the value.
for num in nums:
if nums[abs(num) - 1] < 0:
nums[abs(num) - 1] *= -1
else:
break
return duplicate