-
Notifications
You must be signed in to change notification settings - Fork 43
/
set-mismatch.py
187 lines (160 loc) · 5.13 KB
/
set-mismatch.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
645. Set Mismatch
Easy
You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.
You are given an integer array nums representing the data status of this set after the error.
Find the number that occurs twice and the number that is missing and return them in the form of an array.
Example 1:
Input: nums = [1,2,2,4]
Output: [2,3]
Example 2:
Input: nums = [1,1]
Output: [1,2]
Constraints:
2 <= nums.length <= 104
1 <= nums[i] <= 104
"""
# V0
class Solution(object):
def findErrorNums(self, nums):
N = len(nums)
nset = set(nums)
missing = N * (N + 1) // 2 - sum(nset)
duplicated = sum(nums) - sum(nset)
return [duplicated, missing]
# V0'
from collections import Counter
class Solution(object):
def findErrorNums(self, nums):
_nums = Counter(nums)
duplicate = int([i for i in _nums if _nums[i] > 1][0])
_diff = sum([i for i in range(len(nums)+1)]) - sum(nums)
#print ("duplicate = " + str(duplicate))
return [duplicate, duplicate+_diff]
# V0''
from collections import Counter
class Solution(object):
def findErrorNums(self, nums):
cnt = Counter(nums)
_len = len(nums)
_nums = [x+1 for x in range(_len)]
n_sum = sum([x+1 for x in range(_len)])
repeat = [x for x in list(cnt.keys()) if cnt[x] == 2][0]
miss = n_sum - (sum(nums) - repeat)
# print ("repeat = " + str(repeat))
# print ("miss = " + str(miss))
return [repeat, miss]
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79247916
# http://bookshadow.com/weblog/2017/07/24/leetcode-set-mismatch/
# IDEA : SUM
class Solution(object):
def findErrorNums(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
N = len(nums)
nset = set(nums)
missing = N * (N + 1) // 2 - sum(nset)
duplicated = sum(nums) - sum(nset)
return [duplicated, missing]
### Test case :
s=Solution()
assert s.findErrorNums([1,2,2,4]) == [2,3]
assert s.findErrorNums([1,1,3,4]) == [1,2]
assert s.findErrorNums([]) == [0,0]
assert s.findErrorNums([1,2,3]) == [0,0]
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79247916
# http://bookshadow.com/weblog/2017/07/24/leetcode-set-mismatch/
# IDEA : HASH TABLE
class Solution(object):
def findErrorNums(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
hashs = [0] * len(nums)
missing = -1
for i in range(len(nums)):
hashs[nums[i] - 1] += 1
return [hashs.index(2) + 1, hashs.index(0) + 1]
# V1''
# https://leetcode.com/problems/set-mismatch/discuss/105558/Oneliner-Python
class Solution(object):
def findErrorNums(self, nums):
return [sum(nums) - sum(set(nums)), sum(range(1, len(nums)+1)) - sum(set(nums))]
# V1'''
# https://leetcode.com/problems/set-mismatch/discuss/345631/Multiple-Python-Solution
class Solution:
def findErrorNums(self, nums):
x = sum(nums) - sum(set(nums))
y = sum(range(len(nums)+1))-(sum(nums)-x)
return [x,y]
# V1''''
# https://leetcode.com/problems/set-mismatch/discuss/345631/Multiple-Python-Solution
class Solution:
def findErrorNums(self, nums):
count = [0] * (len(nums)+1)
for x in nums:
count[x] += 1
twice = z = 0
for x in range(1, len(nums)+1):
if count[x] == 2:
twice = x
if count[x] == 0:
z = x
return [twice, z]
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def findErrorNums(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
x_xor_y = 0
for i in range(len(nums)):
x_xor_y ^= nums[i] ^ (i+1)
bit = x_xor_y & ~(x_xor_y-1)
result = [0] * 2
for i, num in enumerate(nums):
result[bool(num & bit)] ^= num
result[bool((i+1) & bit)] ^= i+1
if result[0] not in nums:
result[0], result[1] = result[1], result[0]
return result
# Time: O(n)
# Space: O(1)
class Solution2(object):
def findErrorNums(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
result = [0] * 2
for i in nums:
if nums[abs(i)-1] < 0:
result[0] = abs(i)
else:
nums[abs(i)-1] *= -1
for i in range(len(nums)):
if nums[i] > 0:
result[1] = i+1
else:
nums[i] *= -1
return result
# Time: O(n)
# Space: O(1)
class Solution3(object):
def findErrorNums(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
N = len(nums)
x_minus_y = sum(nums) - N*(N+1)//2
x_plus_y = (sum(x*x for x in nums) - N*(N+1)*(2*N+1)/6) // x_minus_y
return (x_plus_y+x_minus_y) // 2, (x_plus_y-x_minus_y) // 2