| 6 | * @return {number[][]} |
| 7 | */ |
| 8 | var combinationSum = function ( |
| 9 | candidates, |
| 10 | target, |
| 11 | index = 0, |
| 12 | combination = [], |
| 13 | combinations = [], |
| 14 | ) { |
| 15 | const isBaseCase = target < 0; |
| 16 | if (isBaseCase) return combinations; |
| 17 | |
| 18 | const isTarget = target === 0; |
| 19 | if (isTarget) return combinations.push(combination.slice()); |
| 20 | |
| 21 | for (let i = index; i < candidates.length; i++) { |
| 22 | backTrack(candidates, target, i, combination, combinations); |
| 23 | } |
| 24 | |
| 25 | return combinations; |
| 26 | }; |
| 27 | |
| 28 | const backTrack = (candidates, target, i, combination, combinations) => { |
| 29 | combination.push(candidates[i]); |