Skip to content

Commit 1531f13

Browse files
committed
update status/code (Bit_Manipulation/set-mismatch.py)
1 parent 4126a55 commit 1531f13

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@
430430
461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Bit_Manipulation/hamming-distance.py) | _O(1)_ | _O(1)_ | Easy |`fb`| OK* (2)
431431
462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) |[Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Bit_Manipulation/minimum-moves-to-equal-array-elements-ii.py) | _O(n)_ on average | _O(1)_ | Medium || AGAIN*
432432
477 | [Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) |[Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Bit_Manipulation/total-hamming-distance.py) | _O(n)_ | _O(1)_ | Medium |bit manipulation, `fb`| AGAIN*** (not start*) (3)
433-
645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Bit_Manipulation/set-mismatch.py) | _O(n)_ | _O(1)_ | Easy |`amazon`| OK*
433+
645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Bit_Manipulation/set-mismatch.py) | _O(n)_ | _O(1)_ | Easy |`amazon`| OK* (2)
434434
693 | [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Bit_Manipulation/binary-number-with-alternating-bits.py) | _O(1)_ | _O(1)_ | Easy |`trick`| AGAIN*
435435
762 | [Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/) |[Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Bit_Manipulation/prime-number-of-set-bits-in-binary-representation.py) | _O(1)_ | _O(1)_ | Easy |`trick`,`prime number`| AGAIN*
436436
868 | [Binary Gap](https://leetcode.com/problems/binary-gap/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Bit_Manipulation/binary-gap.py) | _O(1)_ | _O(1)_ | Easy |`trick`, `linear scan`| AGAIN*

leetcode_python/Bit_Manipulation/set-mismatch.py

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1+
# 645. Set Mismatch
2+
# Easy
3+
#
4+
#
5+
# Add to List
6+
#
7+
# The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.
8+
#
9+
# Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.
10+
#
11+
# Example 1:
12+
# Input: nums = [1,2,2,4]
13+
# Output: [2,3]
14+
# Note:
15+
# The given array size will in the range [2, 10000].
16+
# The given array's numbers won't have any order.
17+
18+
119
# V0
20+
class Solution(object):
21+
def findErrorNums(self, nums):
22+
N = len(nums)
23+
nset = set(nums)
24+
missing = N * (N + 1) // 2 - sum(nset)
25+
duplicated = sum(nums) - sum(nset)
26+
return [duplicated, missing]
227

3-
# V1'
28+
# V1
429
# https://blog.csdn.net/fuxuemingzhu/article/details/79247916
530
# http://bookshadow.com/weblog/2017/07/24/leetcode-set-mismatch/
631
# IDEA : SUM
@@ -12,10 +37,17 @@ def findErrorNums(self, nums):
1237
"""
1338
N = len(nums)
1439
nset = set(nums)
15-
missing = N * (N + 1) / 2 - sum(nset)
40+
missing = N * (N + 1) // 2 - sum(nset)
1641
duplicated = sum(nums) - sum(nset)
1742
return [duplicated, missing]
18-
43+
44+
### Test case :
45+
s=Solution()
46+
assert s.findErrorNums([1,2,2,4]) == [2,3]
47+
assert s.findErrorNums([1,1,3,4]) == [1,2]
48+
assert s.findErrorNums([]) == [0,0]
49+
assert s.findErrorNums([1,2,3]) == [0,0]
50+
1951
# V1'
2052
# https://blog.csdn.net/fuxuemingzhu/article/details/79247916
2153
# http://bookshadow.com/weblog/2017/07/24/leetcode-set-mismatch/
@@ -32,6 +64,36 @@ def findErrorNums(self, nums):
3264
hashs[nums[i] - 1] += 1
3365
return [hashs.index(2) + 1, hashs.index(0) + 1]
3466

67+
# V1''
68+
# https://leetcode.com/problems/set-mismatch/discuss/105558/Oneliner-Python
69+
class Solution(object):
70+
def findErrorNums(self, nums):
71+
return [sum(nums) - sum(set(nums)), sum(range(1, len(nums)+1)) - sum(set(nums))]
72+
73+
# V1'''
74+
# https://leetcode.com/problems/set-mismatch/discuss/345631/Multiple-Python-Solution
75+
class Solution:
76+
def findErrorNums(self, nums):
77+
x = sum(nums) - sum(set(nums))
78+
y = sum(range(len(nums)+1))-(sum(nums)-x)
79+
return [x,y]
80+
81+
# V1''''
82+
# https://leetcode.com/problems/set-mismatch/discuss/345631/Multiple-Python-Solution
83+
class Solution:
84+
def findErrorNums(self, nums):
85+
count = [0] * (len(nums)+1)
86+
for x in nums:
87+
count[x] += 1
88+
89+
twice = z = 0
90+
for x in range(1, len(nums)+1):
91+
if count[x] == 2:
92+
twice = x
93+
if count[x] == 0:
94+
z = x
95+
return [twice, z]
96+
3597
# V2
3698
# Time: O(n)
3799
# Space: O(1)

0 commit comments

Comments
 (0)