File tree 5 files changed +243
-0
lines changed
solution/0200-0299/0216.Combination Sum III
5 files changed +243
-0
lines changed Original file line number Diff line number Diff line change @@ -222,6 +222,32 @@ function combinationSum3(k: number, n: number): number[][] {
222
222
}
223
223
```
224
224
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
+
225
251
#### Rust
226
252
227
253
``` rust
@@ -457,6 +483,33 @@ function combinationSum3(k: number, n: number): number[][] {
457
483
}
458
484
```
459
485
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
+
460
513
#### C#
461
514
462
515
``` cs
@@ -635,6 +688,39 @@ function bitCount(i: number): number {
635
688
}
636
689
```
637
690
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
+
638
724
#### C#
639
725
640
726
``` cs
Original file line number Diff line number Diff line change @@ -221,6 +221,32 @@ function combinationSum3(k: number, n: number): number[][] {
221
221
}
222
222
```
223
223
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
+
224
250
#### Rust
225
251
226
252
``` rust
@@ -456,6 +482,33 @@ function combinationSum3(k: number, n: number): number[][] {
456
482
}
457
483
```
458
484
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
+
459
512
#### C#
460
513
461
514
``` cs
@@ -634,6 +687,39 @@ function bitCount(i: number): number {
634
687
}
635
688
```
636
689
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
+
637
723
#### C#
638
724
639
725
``` cs
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments