问题描述
给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例
输入: [1,2,2]
输出:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
问题思路
这个其实跟子集是一样的思路。只不过这里是可能含有重复元素的,于是我们要作去重处理。怎么去重? 用dict。
class Solution:
def subsetsWithDup(self, nums):
res = [[]]
pools = []
if not len(nums):
return res
for i in nums:
for j in res[:]:
curs = j[:]
curs.append(i)
tmp = dict()
for k in curs:
if k in tmp:
tmp[k] += 1
else:
tmp[k] = 1
if tmp not in pools:
res.append(curs)
pools.append(tmp)
return res