blob: 80ccfc63a27f17ded9b2940c5d806de84f693dbf (
plain)
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
|
# 90. Subsets II
"""
given an integer array 'nums' that may contain duplicates, return all
possible subsets (the power set). the solution set must not contain duplicate
subsets. return the solution in any order.
"""
class Solution(object):
def subsetWithDup(self, nums):
n = len(nums)
nums.sort()
seen = set()
for mask in range(1 << n):
subset = []
for i in range(n):
bit = (mask >> i) & 1 # get i'th bit of mask
if bit == 1:
subset.append(nums[i])
seen.add(tuple(subset))
return seen
if __name__ == "__main__":
obj = Solution()
print(obj.subsetWithDup([1, 2, 2]))
print(obj.subsetWithDup([0]))
|