diff --git a/solution/0000-0099/0020.Valid Parentheses/README.md b/solution/0000-0099/0020.Valid Parentheses/README.md index de50710ecfec9..90c196ebc2976 100644 --- a/solution/0000-0099/0020.Valid Parentheses/README.md +++ b/solution/0000-0099/0020.Valid Parentheses/README.md @@ -252,6 +252,28 @@ impl Solution { } ``` +### **C#** + +```cs +public class Solution { + public bool IsValid(string s) { + Stack stk = new Stack(); + foreach (var c in s.ToCharArray()) { + if (c == '(') { + stk.Push(')'); + } else if (c == '[') { + stk.Push(']'); + } else if (c == '{') { + stk.Push('}'); + } else if (stk.Count == 0 || stk.Pop() != c) { + return false; + } + } + return stk.Count == 0; + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0020.Valid Parentheses/README_EN.md b/solution/0000-0099/0020.Valid Parentheses/README_EN.md index 577d9fa916a5f..55aed31cf83be 100644 --- a/solution/0000-0099/0020.Valid Parentheses/README_EN.md +++ b/solution/0000-0099/0020.Valid Parentheses/README_EN.md @@ -230,6 +230,28 @@ impl Solution { } ``` +### **C#** + +```cs +public class Solution { + public bool IsValid(string s) { + Stack stk = new Stack(); + foreach (var c in s.ToCharArray()) { + if (c == '(') { + stk.Push(')'); + } else if (c == '[') { + stk.Push(']'); + } else if (c == '{') { + stk.Push('}'); + } else if (stk.Count == 0 || stk.Pop() != c) { + return false; + } + } + return stk.Count == 0; + } +} +``` + ### **...** ```