leetcode 216. Combination Sum III

来源:互联网 发布:创作漫画的软件 编辑:程序博客网 时间:2024/05/29 08:16
class Solution(object):    def combinationSum3(self, k, n):        """        :type k: int        :type n: int        :rtype: List[List[int]]        """        res = list()        self.dfs(res,[],0,1,k,n)        return res    def dfs(self,res,tmp,sum,start,k,n):        if len(tmp) > k:            return        if sum == n and len(tmp) == k:            res.append(tmp[:])            return        for i in range(start,10):            if sum + i > n:                break            tmp.append(i)            self.dfs(res,tmp,sum+i,i+1,k,n)            tmp.pop()

原创粉丝点击