class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res = []
def backtrack(start, comp):
if len(comp) == k:
res.append(comp.copy())
return
for i in range(start, n + 1):
comp.append(i)
backtrack(i + 1, comp)
comp.pop()
backtrack(1, [])
return res
#check all the possible combinations
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = []
def backtrack(start, comb):
if sum(comb) == n and len(comb) == k:
res.append(comb.copy())
return
for i in range(start, 10):
comb.append(i)
backtrack(i + 1, comb)
comb.pop()
backtrack(1, [])
return res