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.
Example:
Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
注意点:空集也是子集
使用count计数,遍历生成某个数字所有的排列list,从数量0开始生成,0时为空集
然后和前面保存的subset组合拼接生成新的subset列表,注意空集拼接是为了保留之前的subset
class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
subset = []
subset.append([])
count = collections.Counter(nums)
for v,num in count.items():
curset = [[v]*i for i in range(num+1)]
subset = [pre+cur for pre in subset for cur in curset]
return subset