class Solution:
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
ret = []
nums.sort()
self.dfs(nums,0,[],ret)
return ret #list(set(ret))
def dfs(self,nums, pos,path,ret):
ret.append(list(path))
for i in range(pos,len(nums)):
if i!=pos and nums[i]==nums[i-1]:
continue
path.append(nums[i])
self.dfs(nums,i+1,path,ret)
path.pop()
方法2:
class Solution:
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
ret = []
nums.sort()
self.dfs(nums,0,[],ret)
return ret #list(set(ret))
def dfs(self,nums, pos,path,ret):
ret.append(path)
for i in range(pos,len(nums)):
if i!=pos and nums[i]==nums[i-1]:
continue
self.dfs(nums,i+1,path+[nums[i]],ret)