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
| class Solution(object): def subsetsWithDup(self, nums): if not nums: return [] res = [] self.dfs(sorted(nums), 0, [], res) return res
def dfs(self, nums, idx, path, res): res.append(path)
for i in xrange(idx, len(nums)): if i > idx and nums[i] == nums[i-1]: continue self.dfs(nums, i+1, path+[nums[i]], res)
def dfs1(self, nums, idx, path, res): res.append(copy.deepcopy(path))
for i in xrange(idx, len(nums)): if i > idx and nums[i] == nums[i-1]: continue
path.append(nums[i]) self.dfs1(nums, i+1, path, res) path.pop()
|