class Solution:
def __init__(self):
self.path = []
self.res = []
self.sum = 0
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
self.backtracking(k, n, 1)
return self.res
def backtracking(self, k, n, start_index):
if self.sum > n:
return
if len(self.path) == k:
if n == self.sum:
self.res.append(self.path[:])
return
for i in range(start_index, 10):
self.path.append(i)
self.sum += i
self.backtracking(k, n, i+1)
self.sum -= i
self.path.pop()