diff --git a/solution/0000-0099/0022.Generate Parentheses/README.md b/solution/0000-0099/0022.Generate Parentheses/README.md index 5dd9d47a628aa..b470955a10c8a 100644 --- a/solution/0000-0099/0022.Generate Parentheses/README.md +++ b/solution/0000-0099/0022.Generate Parentheses/README.md @@ -225,6 +225,33 @@ var generateParenthesis = function (n) { }; ``` +#### C# + +```cs +public class Solution { + private List ans = new List(); + private int n; + + public List GenerateParenthesis(int n) { + this.n = n; + Dfs(0, 0, ""); + return ans; + } + + private void Dfs(int l, int r, string t) { + if (l > n || r > n || l < r) { + return; + } + if (l == n && r == n) { + ans.Add(t); + return; + } + Dfs(l + 1, r, t + "("); + Dfs(l, r + 1, t + ")"); + } +} +``` + #### PHP ```php diff --git a/solution/0000-0099/0022.Generate Parentheses/README_EN.md b/solution/0000-0099/0022.Generate Parentheses/README_EN.md index 6eeb019475ce1..867a20c5fb1d8 100644 --- a/solution/0000-0099/0022.Generate Parentheses/README_EN.md +++ b/solution/0000-0099/0022.Generate Parentheses/README_EN.md @@ -220,6 +220,33 @@ var generateParenthesis = function (n) { }; ``` +#### C# + +```cs +public class Solution { + private List ans = new List(); + private int n; + + public List GenerateParenthesis(int n) { + this.n = n; + Dfs(0, 0, ""); + return ans; + } + + private void Dfs(int l, int r, string t) { + if (l > n || r > n || l < r) { + return; + } + if (l == n && r == n) { + ans.Add(t); + return; + } + Dfs(l + 1, r, t + "("); + Dfs(l, r + 1, t + ")"); + } +} +``` + #### PHP ```php diff --git a/solution/0000-0099/0022.Generate Parentheses/Solution.cs b/solution/0000-0099/0022.Generate Parentheses/Solution.cs new file mode 100644 index 0000000000000..6f6257e62a39e --- /dev/null +++ b/solution/0000-0099/0022.Generate Parentheses/Solution.cs @@ -0,0 +1,22 @@ +public class Solution { + private List ans = new List(); + private int n; + + public List GenerateParenthesis(int n) { + this.n = n; + Dfs(0, 0, ""); + return ans; + } + + private void Dfs(int l, int r, string t) { + if (l > n || r > n || l < r) { + return; + } + if (l == n && r == n) { + ans.Add(t); + return; + } + Dfs(l + 1, r, t + "("); + Dfs(l, r + 1, t + ")"); + } +} \ No newline at end of file