原题
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
代码分析
栈的应用之字符配对。
代码实现
public bool IsValid(string s)
{
if (string.IsNullOrEmpty(s)) return true;
Stack<char> stack = new Stack<char>();
foreach (var item in s)
{
if (item == '(' || item == '{' || item == '[')
{
stack.Push(item);
continue;
}
if (stack.Count == 0) //栈为空,Peek报错,所以要加这个判断
return false;
if (item == ')')
{
if ( stack.Peek() != '(')
return false;
stack.Pop(); //相等的话要出栈
}
else if (item == '}')
{
if ( stack.Peek() != '{')
return false;
stack.Pop();
}
else if (item == ']')
{
if (stack.Peek() != '[')
return false;
stack.Pop();
}
}
return stack.Count==0;
}