Skip to content

Commit 80175eb

Browse files
committed
feat: add js solution to lc problem: No.0216
1 parent 1ec2215 commit 80175eb

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

solution/0200-0299/0216.Combination Sum III/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,33 @@ function combinationSum3(k: number, n: number): number[][] {
483483
}
484484
```
485485

486+
#### JavaScript
487+
488+
```js
489+
function combinationSum3(k, n) {
490+
const ans = [];
491+
const t = [];
492+
const dfs = (i, s) => {
493+
if (s === 0) {
494+
if (t.length === k) {
495+
ans.push(t.slice());
496+
}
497+
return;
498+
}
499+
if (i > 9 || i > s || t.length >= k) {
500+
return;
501+
}
502+
for (let j = i; j <= 9; ++j) {
503+
t.push(j);
504+
dfs(j + 1, s - j);
505+
t.pop();
506+
}
507+
};
508+
dfs(1, n);
509+
return ans;
510+
}
511+
```
512+
486513
#### C#
487514

488515
```cs

solution/0200-0299/0216.Combination Sum III/README_EN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,33 @@ function combinationSum3(k: number, n: number): number[][] {
482482
}
483483
```
484484

485+
#### JavaScript
486+
487+
```js
488+
function combinationSum3(k, n) {
489+
const ans = [];
490+
const t = [];
491+
const dfs = (i, s) => {
492+
if (s === 0) {
493+
if (t.length === k) {
494+
ans.push(t.slice());
495+
}
496+
return;
497+
}
498+
if (i > 9 || i > s || t.length >= k) {
499+
return;
500+
}
501+
for (let j = i; j <= 9; ++j) {
502+
t.push(j);
503+
dfs(j + 1, s - j);
504+
t.pop();
505+
}
506+
};
507+
dfs(1, n);
508+
return ans;
509+
}
510+
```
511+
485512
#### C#
486513

487514
```cs
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function combinationSum3(k, n) {
2+
const ans = [];
3+
const t = [];
4+
const dfs = (i, s) => {
5+
if (s === 0) {
6+
if (t.length === k) {
7+
ans.push(t.slice());
8+
}
9+
return;
10+
}
11+
if (i > 9 || i > s || t.length >= k) {
12+
return;
13+
}
14+
for (let j = i; j <= 9; ++j) {
15+
t.push(j);
16+
dfs(j + 1, s - j);
17+
t.pop();
18+
}
19+
};
20+
dfs(1, n);
21+
return ans;
22+
}

0 commit comments

Comments
 (0)