LeetCode216. 组合总和 III
- 题目链接
- 代码
题目链接
https://leetcode.cn/problems/combination-sum-iii/
代码
class Solution:def combinationSum3(self, k: int, n: int) -> List[List[int]]:result=[]self.backtracking(n, k, 0, 1, [], result)return resultdef backtracking(self, targetsum, k, currentsum, startindex, path, result):if currentsum > targetsum:return if len(path) == k:if currentsum == targetsum:result.append(path[:])returnfor i in range(startindex, 9 - (k - len(path)) + 2):currentsum += ipath.append(i)self.backtracking(targetsum, k, currentsum, i + 1, path, result)currentsum -= ipath.pop()