-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path90.py
41 lines (30 loc) · 1.06 KB
/
90.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
# Problem link: https://leetcode.com/problems/subsets-ii/
class Solution:
def getSubset(self, mask: int) -> List[int]:
res = []
for i in range(self.n):
if mask & (1 << i):
res.append(self.nums[i])
res.sort()
return res
def equalLists(self, a: List[int], b: List[int]) -> bool:
if len(a) != len(b):
return False
for i in range(len(a)):
if a[i] != b[i]:
return False
return True
def contains(self, subset: List[int]) -> bool:
for l in self.ans:
if self.equalLists(l, subset):
return True
return False
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
self.n = len(nums)
self.nums = nums
self.ans = []
for mask in range((1 << self.n)):
subset = self.getSubset(mask)
if not self.contains(subset):
self.ans.append(subset)
return self.ans