|
1 | 1 | public class Solution { |
2 | 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; |
| 3 | + Stack<char> stk = new Stack<char>(); |
| 4 | + foreach (var c in s.ToCharArray()) { |
| 5 | + if (c == '(') { |
| 6 | + stk.Push(')'); |
| 7 | + } else if (c == '[') { |
| 8 | + stk.Push(']'); |
| 9 | + } else if (c == '{') { |
| 10 | + stk.Push('}'); |
| 11 | + } else if (stk.Count == 0 || stk.Pop() != c) { |
| 12 | + return false; |
| 13 | + } |
| 14 | + } |
| 15 | + return stk.Count == 0; |
15 | 16 | } |
16 | 17 | } |
0 commit comments