#216
Combination Sum III
pupil · 515 · lc medium +28 · verified · 73% accepted · 6,613 likes · top 83%
Description
Identify every combination of exactly k distinct digits from 1–9 that adds up to n.
- Each digit may appear at most once per combination.
Return all valid combinations (no duplicates; any order is fine).
Example 1:
Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.
Example 2:
Input: k = 3, n = 9
Output: [[1,2,6],[1,3,5],[2,3,4]]
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input: k = 4, n = 1
Output: []
Explanation: There are no valid combinations.
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
Code
1
2
3