216Combination Sum III

来源:互联网 发布:沈阳网络系统安全技术 编辑:程序博客网 时间:2024/06/05 10:45
class Solution(object):
    def combinationSum3(self, k, n):
        """
        :type k: int
        :type n: int
        :rtype: List[List[int]]
        """
        cadicates=[x for x in range(1,10)]
        res=[]
        line=[]
        self.helper(cadicates,k,n,res,line)
        return res
        
    def helper(self,cadicates,k,n,res,line):
        if len(line)==k:
            if n==0:
                res.append([x for x in line])
            return
        for i,x in enumerate(cadicates):
            line.append(x)
            self.helper(cadicates[i+1:],k,n-x,res,line)
            line.pop()
原创粉丝点击