Combination sum
Mediumbacktrackingrecursionarray
candidates holds distinct positive numbers. Return every combination summing to target, where each number may repeat any number of times. A combination is a set (order inside it doesn't matter), and the order of combinations doesn't matter either.
Example 1
Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]]
Example 2
Input: candidates = [2,3,5], target = 8 Output: [[2,2,2,2],[2,3,3],[3,5]]
Example 3
Input: candidates = [2], target = 1 Output: []
Constraints
- 1 ≤ candidates.length ≤ 30
- 2 ≤ candidates[i] ≤ 40, distinct
- 1 ≤ target ≤ 40
Submit checks your solution against 9 tests, including hidden ones.
/**
* @param {int[]} candidates
* @param {int} target
* @return {int[][]}
*/
function combinationSum(candidates, target) {
}