From 97fef47eee9e85b1573f7c31181e5cdf7cf46ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Velimir=20=C4=90urkovi=C4=87?= Date: Fri, 7 Feb 2025 17:23:05 +0100 Subject: [PATCH] feat: add solution to lc problem: No.0022 --- .../0022.Generate Parentheses/README.md | 27 +++++++++++++++++++ .../0022.Generate Parentheses/README_EN.md | 27 +++++++++++++++++++ .../0022.Generate Parentheses/Solution.cs | 22 +++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 solution/0000-0099/0022.Generate Parentheses/Solution.cs 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