Skip to content

Commit 67e2ec0

Browse files
authored
feat: add js/ts solutions to lc problem: No.0022 (doocs#3242)
1 parent 40bd805 commit 67e2ec0

File tree

4 files changed

+117
-17
lines changed

4 files changed

+117
-17
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+
}

0 commit comments

Comments
 (0)