Title: identify the number of combinations of n and k is the sum of all. Composition may only contain 1-- 9 positive integer, and repeatable number does not exist in each combination.
Note: All figures are positive integers. Solution Set can not contain duplicate combinations thereof.
Example 1: Input: k = 3, n = 7 Output: [[2,4]]
Source: https: //leetcode-cn.com/problems/combination-sum-iii/
Act one: their own code
Ideas: The first two questions with similar, but different pruning conditions only, this problem which clearly requires the length of the resulting array, this condition is actually pruning conditions
from Typing Import List class Solution: DEF combinationSum3 (Self, K: int, n-: int) -> List [List [int]]: Results = [] the nums = [R & lt +. 1 for R & lt in Range (. 9 )] # A each stored list generated, the number stored for the nums traversal, DEF BackTrack (a = [], the nums = the nums,): # pruning conditions, if long enough, and need not be judged, a direct access to the traversing IF len (a) < K: Pass # If the condition is satisfied, then the length must be a k, n are as long as return value, and ends the next traversal elif SUM (a) == n: Print (A) results.append (A) return for I, J in the enumerate (the nums): # (len (A) <K), this condition must have, such as input (2, 18), if this condition is not, # need to be limited to the above if statement, which limits less time if (SUM (a) + J <= n-) & (len (a) < K): BackTrack (a + [J], the nums [I +. 1 :]) # If the condition is not satisfied if there is one, terminate the loop, as the number nums is ascending sort, # n-number is not satisfied, then, n + 1 must not satisfied after the direct termination circulating the else : BREAK BackTrack () return Results IF __name__ == " __main__" : Duixiang = Solution () A = duixiang.combinationSum3 (2, 18 is ) Print ( ' U ' , A)