题目描述:
题解:回溯
1.类似于leetcode77. 组合python_ganggang的博客-CSDN博客
2.res保存最终结果,path记录当前搜索路径,每次将path添加到res中。
3.为了避免重复组合下一层搜索从下一个位置开始,此题也同样不需要used数组记录使用情况。
class Solution(object): def subsets(self, nums): res = [] used = [0 for i in range(len(nums))] self.dfs(0,nums,res,[],used) return res def dfs(self,idx,nums,res,path,used): res.append(path[:]) for i in range(idx,len(nums)): path.append(nums[i]) idx = idx+1 self.dfs(idx,nums,res,path,used) path.pop()