Skip to content

Commit a909bb4

Browse files
authored
Merge branch 'main' into patch-6
2 parents c556cdc + 52ea485 commit a909bb4

File tree

88 files changed

+1922
-2082
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1922
-2082
lines changed

solution/0000-0099/0022.Generate Parentheses/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,46 @@ class Solution {
256256

257257
<!-- tabs:end -->
258258

259+
<!-- solution:start -->
260+
261+
### 方法二:递归
262+
263+
<!-- tabs:start -->
264+
265+
#### TypeScript
266+
267+
```ts
268+
function generateParenthesis(n: number): string[] {
269+
if (n === 1) return ['()'];
270+
271+
return [
272+
...new Set(
273+
generateParenthesis(n - 1).flatMap(s =>
274+
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
275+
),
276+
),
277+
];
278+
}
279+
```
280+
281+
#### JavaScript
282+
283+
```js
284+
function generateParenthesis(n) {
285+
if (n === 1) return ['()'];
286+
287+
return [
288+
...new Set(
289+
generateParenthesis(n - 1).flatMap(s =>
290+
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
291+
),
292+
),
293+
];
294+
}
295+
```
296+
297+
<!-- tabs:end -->
298+
259299
<!-- solution:end -->
260300

261301
<!-- problem:end -->

solution/0000-0099/0022.Generate Parentheses/README_EN.md

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,25 +148,21 @@ func generateParenthesis(n int) (ans []string) {
148148
#### TypeScript
149149

150150
```ts
151-
impl Solution {
152-
pub fn generate_parenthesis(n: i32) -> Vec<String> {
153-
let mut ans = Vec::new();
154-
155-
fn dfs(ans: &mut Vec<String>, l: i32, r: i32, t: String, n: i32) {
156-
if l > n || r > n || l < r {
157-
return;
158-
}
159-
if l == n && r == n {
160-
ans.push(t);
161-
return;
162-
}
163-
dfs(ans, l + 1, r, format!("{}(", t), n);
164-
dfs(ans, l, r + 1, format!("{})", t), n);
151+
function generateParenthesis(n: number): string[] {
152+
function dfs(l, r, t) {
153+
if (l > n || r > n || l < r) {
154+
return;
165155
}
166-
167-
dfs(&mut ans, 0, 0, String::new(), n);
168-
ans
156+
if (l == n && r == n) {
157+
ans.push(t);
158+
return;
159+
}
160+
dfs(l + 1, r, t + '(');
161+
dfs(l, r + 1, t + ')');
169162
}
163+
let ans = [];
164+
dfs(0, 0, '');
165+
return ans;
170166
}
171167
```
172168

@@ -257,4 +253,46 @@ class Solution {
257253

258254
<!-- solution:end -->
259255

256+
<!-- solution:start -->
257+
258+
### Solution 2: Recursion
259+
260+
<!-- tabs:start -->
261+
262+
#### TypeScript
263+
264+
```ts
265+
function generateParenthesis(n: number): string[] {
266+
if (n === 1) return ['()'];
267+
268+
return [
269+
...new Set(
270+
generateParenthesis(n - 1).flatMap(s =>
271+
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
272+
),
273+
),
274+
];
275+
}
276+
```
277+
278+
#### JavaScript
279+
280+
```js
281+
function generateParenthesis(n) {
282+
if (n === 1) return ['()'];
283+
284+
return [
285+
...new Set(
286+
generateParenthesis(n - 1).flatMap(s =>
287+
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
288+
),
289+
),
290+
];
291+
}
292+
```
293+
294+
<!-- tabs:end -->
295+
296+
<!-- solution:end -->
297+
260298
<!-- problem:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function generateParenthesis(n) {
2+
if (n === 1) return ['()'];
3+
4+
return [
5+
...new Set(
6+
generateParenthesis(n - 1).flatMap(s =>
7+
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
8+
),
9+
),
10+
];
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function generateParenthesis(n: number): string[] {
2+
if (n === 1) return ['()'];
3+
4+
return [
5+
...new Set(
6+
generateParenthesis(n - 1).flatMap(s =>
7+
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
8+
),
9+
),
10+
];
11+
}

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

Lines changed: 86 additions & 0 deletions
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

Lines changed: 86 additions & 0 deletions
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
Lines changed: 21 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)