From 52aae558c9b71443b84913a8bcda0334e3e6f9ec Mon Sep 17 00:00:00 2001 From: Pandurang Lad Date: Sun, 5 Nov 2023 13:35:21 +0530 Subject: [PATCH 1/2] feat: added solutions to lc problem: No.0020 Created file solution.cs for C# solution to lc problem: No.0020 --- .../0020.Valid Parentheses/solutions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 solution/0000-0099/0020.Valid Parentheses/solutions.cs diff --git a/solution/0000-0099/0020.Valid Parentheses/solutions.cs b/solution/0000-0099/0020.Valid Parentheses/solutions.cs new file mode 100644 index 0000000000000..b35e035c0a336 --- /dev/null +++ b/solution/0000-0099/0020.Valid Parentheses/solutions.cs @@ -0,0 +1,16 @@ +public class Solution { + public bool IsValid(string s) { + Stack ch = new Stack(); + foreach (var item in s.ToCharArray()) + if (item == '(') + ch.Push(')'); + else if (item == '[') + ch.Push(']'); + else if (item == '{') + ch.Push('}'); + else if (ch.Count == 0 || ch.Pop() != item) + return false; + + return ch.Count == 0; + } +} From bac99c1d5ce4108327a6127b26e7b03af964d10a Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 5 Nov 2023 23:01:39 +0800 Subject: [PATCH 2/2] Update solutions.cs --- .../0020.Valid Parentheses/solutions.cs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/solution/0000-0099/0020.Valid Parentheses/solutions.cs b/solution/0000-0099/0020.Valid Parentheses/solutions.cs index b35e035c0a336..f0b79f30fbc19 100644 --- a/solution/0000-0099/0020.Valid Parentheses/solutions.cs +++ b/solution/0000-0099/0020.Valid Parentheses/solutions.cs @@ -1,16 +1,17 @@ public class Solution { public bool IsValid(string s) { - Stack ch = new Stack(); - foreach (var item in s.ToCharArray()) - if (item == '(') - ch.Push(')'); - else if (item == '[') - ch.Push(']'); - else if (item == '{') - ch.Push('}'); - else if (ch.Count == 0 || ch.Pop() != item) - return false; - - return ch.Count == 0; + 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; } }