题目描述:
方法一:回溯
class Solution: def subsets(self, nums: List[int]) -> List[List[int]]: if not sum: return [] res = [] n = len(nums) def backtrack(idx,temp_list): res.append(temp_list) for i in range(idx,n): backtrack(i+1,temp_list+[nums[i]]) backtrack(0,[]) return res
方法二:迭代
class Solution: def subsets(self, nums: List[int]) -> List[List[int]]: res = [[]] for i in nums: res = res + [[i] + num for num in res] return res