216. Combination Sum III
Question
Find all valid combinations of
k
numbers that sum up ton
such that the following conditions are true:
- Only numbers
1
through9
are used.- Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
Solution
回溯,创建一个成员变量visited[]记录访问情况。
剪枝优化,遍历所有可选择的数字,将其加和,如果加和大于target则返回。
剪枝优化,每层级遍历的范围大于传入列表中的最后一个元素。
回溯,向下递归。
如果k等于0并且sum等于target则添加arr到列表ret中。
Code
1 | class Solution { |
216. Combination Sum III
https://xuanhe95.github.io/2022/05/10/216-Combination-Sum-III/