We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 52aae55 commit bac99c1Copy full SHA for bac99c1
solution/0000-0099/0020.Valid Parentheses/solutions.cs
@@ -1,16 +1,17 @@
1
public class Solution {
2
public bool IsValid(string s) {
3
- Stack<char> ch = new Stack<char>();
4
- foreach (var item in s.ToCharArray())
5
- if (item == '(')
6
- ch.Push(')');
7
- else if (item == '[')
8
- ch.Push(']');
9
- else if (item == '{')
10
- ch.Push('}');
11
- else if (ch.Count == 0 || ch.Pop() != item)
12
- return false;
13
-
14
- return ch.Count == 0;
+ Stack<char> stk = new Stack<char>();
+ 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;
+ }
15
+ return stk.Count == 0;
16
}
17
0 commit comments