Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions solution/0000-0099/0022.Generate Parentheses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,46 @@ class Solution {

<!-- tabs:end -->

<!-- solution:start -->

### 方法二:递归

<!-- tabs:start -->

#### TypeScript

```ts
function generateParenthesis(n: number): string[] {
if (n === 1) return ['()'];

return [
...new Set(
generateParenthesis(n - 1).flatMap(s =>
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
),
),
];
}
```

#### JavaScript

```js
function generateParenthesis(n) {
if (n === 1) return ['()'];

return [
...new Set(
generateParenthesis(n - 1).flatMap(s =>
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
),
),
];
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
72 changes: 55 additions & 17 deletions solution/0000-0099/0022.Generate Parentheses/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,25 +148,21 @@ func generateParenthesis(n int) (ans []string) {
#### TypeScript

```ts
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut ans = Vec::new();

fn dfs(ans: &mut Vec<String>, l: i32, r: i32, t: String, n: i32) {
if l > n || r > n || l < r {
return;
}
if l == n && r == n {
ans.push(t);
return;
}
dfs(ans, l + 1, r, format!("{}(", t), n);
dfs(ans, l, r + 1, format!("{})", t), n);
function generateParenthesis(n: number): string[] {
function dfs(l, r, t) {
if (l > n || r > n || l < r) {
return;
}

dfs(&mut ans, 0, 0, String::new(), n);
ans
if (l == n && r == n) {
ans.push(t);
return;
}
dfs(l + 1, r, t + '(');
dfs(l, r + 1, t + ')');
}
let ans = [];
dfs(0, 0, '');
return ans;
}
```

Expand Down Expand Up @@ -257,4 +253,46 @@ class Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Recursion

<!-- tabs:start -->

#### TypeScript

```ts
function generateParenthesis(n: number): string[] {
if (n === 1) return ['()'];

return [
...new Set(
generateParenthesis(n - 1).flatMap(s =>
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
),
),
];
}
```

#### JavaScript

```js
function generateParenthesis(n) {
if (n === 1) return ['()'];

return [
...new Set(
generateParenthesis(n - 1).flatMap(s =>
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
),
),
];
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
11 changes: 11 additions & 0 deletions solution/0000-0099/0022.Generate Parentheses/Solution2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function generateParenthesis(n) {
if (n === 1) return ['()'];

return [
...new Set(
generateParenthesis(n - 1).flatMap(s =>
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
),
),
];
}
11 changes: 11 additions & 0 deletions solution/0000-0099/0022.Generate Parentheses/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function generateParenthesis(n: number): string[] {
if (n === 1) return ['()'];

return [
...new Set(
generateParenthesis(n - 1).flatMap(s =>
Array.from(s, (_, i) => s.slice(0, i) + '()' + s.slice(i)),
),
),
];
}