class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
path = []
res =[]
self.dfs(nums, path, res, start=0)
return res
def dfs(self, nums, path, res, start):
res.append(path[:])
if start == len(nums):
return
for i in range(start, len(nums)):
path.append(nums[i])
self.dfs(nums, path, res, i+1)
path.pop()
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
path = []
res = []
nums.sort()
self.dfs(nums, path, res, start=0)
return res
def dfs(self, nums, path, res, start):
res.append(path[:])
if start >= len(nums):
return
for i in range(start, len(nums)):
if i > start and nums[i] == nums[i-1]: # 注意这里i > start, 而不是 i > 0
continue
path.append(nums[i])
self.dfs(nums, path, res, i+1) # 注意是i+1,不是start+1
path.pop()