class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
self.combine = []
self.ans = []
def backtrack(idx,target):
if target < 0:
return
if len(self.combine)==k:
if target == 0:
self.ans.append(list(self.combine))
return
i = idx
while i <= (9-(k-len(self.combine))+1):
self.combine.append(i)
backtrack(i+1,target-i)
self.combine.pop()
i += 1
backtrack(1,n)
return self.ans
216.组合总和III
最新推荐文章于 2024-06-14 11:20:52 发布