Skip to content

Commit 42775e9

Browse files
committed
feat: add ts solution to lc problem: No.0022
1 parent 8eefa28 commit 42775e9

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

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

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

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

259+
<!-- solution:start -->
260+
261+
### Solution 2: Recursion
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+
<!-- tabs:end -->
282+
259283
<!-- solution:end -->
260284

261285
<!-- problem:end -->

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,30 @@ class Solution {
253253

254254
<!-- solution:end -->
255255

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+
<!-- tabs:end -->
279+
280+
<!-- solution:end -->
281+
256282
<!-- problem:end -->
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+
}

0 commit comments

Comments
 (0)