-
Notifications
You must be signed in to change notification settings - Fork 20
/
SubsetsII.py
51 lines (41 loc) · 1.04 KB
/
SubsetsII.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
42
43
44
45
46
47
48
49
50
51
# Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
#
# Note: The solution set must not contain duplicate subsets.
#
# For example,
# If nums = [1,2,2], a solution is:
#
# [
# [2],
# [1],
# [1,2,2],
# [2,2],
# [1,2],
# []
# ]
#
# Python, Python3 all accepted.
class SubsetsII:
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if nums is None or len(nums) == 0:
return []
lists = []
if len(nums) == 1:
lists.append([])
# Add the empty list.
lists.append([nums[0]])
return lists
nums = sorted(nums)
for lst in self.subsetsWithDup(nums[0:len(nums) - 1]):
li = []
li.extend(lst)
li.append(nums[len(nums) - 1])
if not lists.__contains__(li):
lists.append(li)
if not lists.__contains__(lst):
lists.append(lst)
return lists