美文网首页
2019-05-31LeetCode216. 组合总和 III

2019-05-31LeetCode216. 组合总和 III

作者: mztkenan | 来源:发表于2019-05-31 22:22 被阅读0次

20min 错误在于分支搞不清

    def combinationSum3(self, k: int, n: int) -> List[List[int]]:
        res=[]
        self.dfs(1,[],res,k,n)
        return res

    def dfs(self,start,path,res,k,n):
        if len(path)==k:
            if n==0:res.append(path+[])
            return
        for i in range(start,10):  #错在i与start分不清
            path.append(i)
            self.dfs(i+1, path, res, k, n - i)
            path.pop()

相关文章

网友评论

      本文标题:2019-05-31LeetCode216. 组合总和 III

      本文链接:https://www.haomeiwen.com/subject/redntctx.html