Skip to content

Commit

Permalink
update status/code (Bit_Manipulation/set-mismatch.py)
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 31, 2020
1 parent 4126a55 commit 1531f13
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@
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)
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*
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)
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*
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)
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*
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*
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*
Expand Down
68 changes: 65 additions & 3 deletions leetcode_python/Bit_Manipulation/set-mismatch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
# 645. Set Mismatch
# Easy
#
#
# Add to List
#
# 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.
#
# 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.
#
# Example 1:
# Input: nums = [1,2,2,4]
# Output: [2,3]
# Note:
# The given array size will in the range [2, 10000].
# The given array's numbers won't have any order.


# 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]

# V1'
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79247916
# http://bookshadow.com/weblog/2017/07/24/leetcode-set-mismatch/
# IDEA : SUM
Expand All @@ -12,10 +37,17 @@ def findErrorNums(self, nums):
"""
N = len(nums)
nset = set(nums)
missing = N * (N + 1) / 2 - sum(nset)
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/
Expand All @@ -32,6 +64,36 @@ def findErrorNums(self, 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)
Expand Down

0 comments on commit 1531f13

Please sign in to comment.