Combination Sum III

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.

  • All numbers will be positive integers

  • The solution set must not contain duplicate combinations.

Example

Example 1:

Input: k = 3, n = 7

Output:
 [[1,2,4]]

Example 2:

Input:
k = 3, n = 9

Output:
 [[1,2,6], [1,3,5], [2,3,4]]

Note

组合从n个数中选k个数,n只能是[1, 9]闭区间的值,起始start为1,DFS循环遍历

当k和n都为0的时候,递归退出

注意剪枝:n小于0的情况或者在循环中n小于i

Code

Last updated