216. 组合总和 III

找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。

说明:

所有数字都是正整数。
解集不能包含重复的组合。 
示例 1:

输入: k = 3, n = 7
输出: [[1,2,4]]
示例 2:

输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/combination-sum-iii

递归backtracking,

class Solution:
    def combinationSum3(self, k: int, n: int) -> List[List[int]]:
        if n/k>9:return []
        return self.find(k,n,1def find(self,k,n,minn):#minn:to store the smallest number that can be selected to ensure no duplicate numbers
        if k==1:
            if n>=minn and n<10:return [[n]]
            return []
        res=[]
        i=minn
        while(i<n and i<10):
            a=self.find(k-1,n-i,i+1)
            if(a!=[]):
                for j in a:
                    res.append([i]+j)
            i+=1
        return res
原文地址:https://www.cnblogs.com/xxxsans/p/13650320.html