Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bac99c1

Browse files
Update solutions.cs
1 parent 52aae55 commit bac99c1

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
public class Solution {
22
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;
1516
}
1617
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /