File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ # def subsetsWithDup(self, nums):
3
+ # """
4
+ # :type nums: List[int]
5
+ # :rtype: List[List[int]]
6
+ # """
7
+ # nums.sort()
8
+ # res = []
9
+ # for i in range(1 << len(nums)):
10
+ # res.append(self.get_subsets(nums, i))
11
+ # # remove duplicate
12
+ # final_res = {}
13
+ # for subset in res:
14
+ # hash_key = ''.join([str(t) for t in subset])
15
+ # try:
16
+ # final_res[hash_key]
17
+ # except:
18
+ # final_res[hash_key] = subset
19
+ # return final_res.values()
20
+ #
21
+ # def get_subsets(self, nums, magic):
22
+ # res = []
23
+ # for i in range(len(nums)):
24
+ # if (1 << i) & magic != 0:
25
+ # res.append(nums[i])
26
+ # return res
27
+
28
+ def subsetsWithDup (self , nums ):
29
+ nums .sort ()
30
+ res = [[]]
31
+ begin = 0
32
+ for index in range (len (nums )):
33
+ if index == 0 or nums [index ] != nums [index - 1 ]:
34
+ # generate all
35
+ begin = 0
36
+ size = len (res )
37
+ # use existing subsets to generate new subsets
38
+ for j in range (begin , size ):
39
+ curr = list (res [j ])
40
+ curr .append (nums [index ])
41
+ res .append (curr )
42
+ # avoid duplicate subsets
43
+ begin = size
44
+ return res
You can’t perform that action at this time.
0 commit comments