| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,4 +24,32 @@ function helper(numbers, ret, k, target, item, index) { | |||
| 24 | 24 | helper(numbers, ret, k, target - numbers[i], item, i + 1); | |
| 25 | 25 | item.pop(); | |
| 26 | 26 | } | |
| 27 | + } | ||
| 28 | + | ||
| 29 | + // Similar Solution | ||
| 30 | + /** | ||
| 31 | + * @param {number} k | ||
| 32 | + * @param {number} n | ||
| 33 | + * @return {number[][]} | ||
| 34 | + */ | ||
| 35 | + var combinationSum3 = function (k, n) { | ||
| 36 | + var ret = []; | ||
| 37 | + combinationSum3Builder(k, n, ret, [], 1); | ||
| 38 | + return ret; | ||
| 39 | + }; | ||
| 40 | + | ||
| 41 | + function combinationSum3Builder(k, n, ret, item, index) { | ||
| 42 | + if (item.length > k || n < 0) { | ||
| 43 | + return; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + if (n === 0 && item.length === k) { | ||
| 47 | + ret.push(item.slice()); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + for (var i = index; i <= 9; i++) { | ||
| 51 | + item.push(i); | ||
| 52 | + combinationSum3Builder(k, n - i, ret, item, i + 1); | ||
| 53 | + item.pop(); | ||
| 54 | + } | ||
| 27 | 55 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments