题目描述
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Note:
- All numbers will be positive integers.
- The solution set must not contain duplicate combinations.
Example 1:
1 | Input: k = 3, n = 7 |
Example 2:
1 | Input: k = 3, n = 9 |
使用DFS查找可以很快找到所有的组合。
代码实现
1 | import copy |