Skip to content

Commit 0620cfb

Browse files
authored
feat: add js solution to lc problem: No.0216 (#3244)
1 parent ea133c3 commit 0620cfb

File tree

5 files changed

+243
-0
lines changed

5 files changed

+243
-0
lines changed

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

+86
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,32 @@ function combinationSum3(k: number, n: number): number[][] {
222222
}
223223
```
224224

225+
#### JavaScript
226+
227+
```js
228+
function combinationSum3(k, n) {
229+
const ans = [];
230+
const t = [];
231+
const dfs = (i, s) => {
232+
if (s === 0) {
233+
if (t.length === k) {
234+
ans.push(t.slice());
235+
}
236+
return;
237+
}
238+
if (i > 9 || i > s || t.length >= k) {
239+
return;
240+
}
241+
t.push(i);
242+
dfs(i + 1, s - i);
243+
t.pop();
244+
dfs(i + 1, s);
245+
};
246+
dfs(1, n);
247+
return ans;
248+
}
249+
```
250+
225251
#### Rust
226252

227253
```rust
@@ -457,6 +483,33 @@ function combinationSum3(k: number, n: number): number[][] {
457483
}
458484
```
459485

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+
460513
#### C#
461514

462515
```cs
@@ -635,6 +688,39 @@ function bitCount(i: number): number {
635688
}
636689
```
637690

691+
#### JavaScript
692+
693+
```js
694+
function combinationSum3(k, n) {
695+
const ans = [];
696+
for (let mask = 0; mask < 1 << 9; ++mask) {
697+
if (bitCount(mask) === k) {
698+
const t = [];
699+
let s = 0;
700+
for (let i = 0; i < 9; ++i) {
701+
if (mask & (1 << i)) {
702+
t.push(i + 1);
703+
s += i + 1;
704+
}
705+
}
706+
if (s === n) {
707+
ans.push(t);
708+
}
709+
}
710+
}
711+
return ans;
712+
}
713+
714+
function bitCount(i) {
715+
i = i - ((i >>> 1) & 0x55555555);
716+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
717+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
718+
i = i + (i >>> 8);
719+
i = i + (i >>> 16);
720+
return i & 0x3f;
721+
}
722+
```
723+
638724
#### C#
639725

640726
```cs

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

+86
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,32 @@ function combinationSum3(k: number, n: number): number[][] {
221221
}
222222
```
223223

224+
#### JavaScript
225+
226+
```js
227+
function combinationSum3(k, n) {
228+
const ans = [];
229+
const t = [];
230+
const dfs = (i, s) => {
231+
if (s === 0) {
232+
if (t.length === k) {
233+
ans.push(t.slice());
234+
}
235+
return;
236+
}
237+
if (i > 9 || i > s || t.length >= k) {
238+
return;
239+
}
240+
t.push(i);
241+
dfs(i + 1, s - i);
242+
t.pop();
243+
dfs(i + 1, s);
244+
};
245+
dfs(1, n);
246+
return ans;
247+
}
248+
```
249+
224250
#### Rust
225251

226252
```rust
@@ -456,6 +482,33 @@ function combinationSum3(k: number, n: number): number[][] {
456482
}
457483
```
458484

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+
459512
#### C#
460513

461514
```cs
@@ -634,6 +687,39 @@ function bitCount(i: number): number {
634687
}
635688
```
636689

690+
#### JavaScript
691+
692+
```js
693+
function combinationSum3(k, n) {
694+
const ans = [];
695+
for (let mask = 0; mask < 1 << 9; ++mask) {
696+
if (bitCount(mask) === k) {
697+
const t = [];
698+
let s = 0;
699+
for (let i = 0; i < 9; ++i) {
700+
if (mask & (1 << i)) {
701+
t.push(i + 1);
702+
s += i + 1;
703+
}
704+
}
705+
if (s === n) {
706+
ans.push(t);
707+
}
708+
}
709+
}
710+
return ans;
711+
}
712+
713+
function bitCount(i) {
714+
i = i - ((i >>> 1) & 0x55555555);
715+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
716+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
717+
i = i + (i >>> 8);
718+
i = i + (i >>> 16);
719+
return i & 0x3f;
720+
}
721+
```
722+
637723
#### C#
638724

639725
```cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
t.push(i);
15+
dfs(i + 1, s - i);
16+
t.pop();
17+
dfs(i + 1, s);
18+
};
19+
dfs(1, n);
20+
return ans;
21+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function combinationSum3(k, n) {
2+
const ans = [];
3+
for (let mask = 0; mask < 1 << 9; ++mask) {
4+
if (bitCount(mask) === k) {
5+
const t = [];
6+
let s = 0;
7+
for (let i = 0; i < 9; ++i) {
8+
if (mask & (1 << i)) {
9+
t.push(i + 1);
10+
s += i + 1;
11+
}
12+
}
13+
if (s === n) {
14+
ans.push(t);
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
21+
function bitCount(i) {
22+
i = i - ((i >>> 1) & 0x55555555);
23+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
24+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
25+
i = i + (i >>> 8);
26+
i = i + (i >>> 16);
27+
return i & 0x3f;
28+
}

0 commit comments

Comments
 (0)