- 有效的括号
public class Solution
{
Dictionary<char, char> pairs = new Dictionary<char, char>();
public Solution()
{
pairs[')'] = '(';
pairs[']'] = '[';
pairs['}'] = '{';
}
public bool IsValid(string s)
{
Stack<char> stack = new Stack<char>();
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (pairs.ContainsKey(c))
{
if (stack.Count == 0) return false;
if (stack.Pop() != pairs[c]) return false;
}
else stack.Push(c);
}
return stack.Count == 0;
}
}