diff --git a/solution/0000-0099/0022.Generate Parentheses/README.md b/solution/0000-0099/0022.Generate Parentheses/README.md index ed6a2a137616d..5dd9d47a628aa 100644 --- a/solution/0000-0099/0022.Generate Parentheses/README.md +++ b/solution/0000-0099/0022.Generate Parentheses/README.md @@ -256,6 +256,46 @@ class Solution { + + +### 方法二:递归 + + + +#### 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)), + ), + ), + ]; +} +``` + + + diff --git a/solution/0000-0099/0022.Generate Parentheses/README_EN.md b/solution/0000-0099/0022.Generate Parentheses/README_EN.md index 1e658a09e2759..6eeb019475ce1 100644 --- a/solution/0000-0099/0022.Generate Parentheses/README_EN.md +++ b/solution/0000-0099/0022.Generate Parentheses/README_EN.md @@ -148,25 +148,21 @@ func generateParenthesis(n int) (ans []string) { #### TypeScript ```ts -impl Solution { - pub fn generate_parenthesis(n: i32) -> Vec { - let mut ans = Vec::new(); - - fn dfs(ans: &mut Vec, 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; } ``` @@ -257,4 +253,46 @@ class Solution { + + +### Solution 2: Recursion + + + +#### 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)), + ), + ), + ]; +} +``` + + + + + diff --git a/solution/0000-0099/0022.Generate Parentheses/Solution2.js b/solution/0000-0099/0022.Generate Parentheses/Solution2.js new file mode 100644 index 0000000000000..a32e4742c2166 --- /dev/null +++ b/solution/0000-0099/0022.Generate Parentheses/Solution2.js @@ -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)), + ), + ), + ]; +} diff --git a/solution/0000-0099/0022.Generate Parentheses/Solution2.ts b/solution/0000-0099/0022.Generate Parentheses/Solution2.ts new file mode 100644 index 0000000000000..b13d03cc969ec --- /dev/null +++ b/solution/0000-0099/0022.Generate Parentheses/Solution2.ts @@ -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)), + ), + ), + ]; +}