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], [] ]当有重复数字时,所有的集合结果,主要是去重,和Subsets差不多的做法
class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = [[]]
nums.sort()
for i in range(len(nums)):
if i == 0 or nums[i] != nums[i-1]:
length = len(res)
for j in range(len(res)-length, len(res)):
res.append(res[j] + [nums[i]])
return res