题目:
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.
利用栈来存储 ( [ { ,当出现 ) ] },弹出栈顶元素,如果与之对应则继续对比,如果出现不对应则说明是非法匹配,最后返回栈的大小是否为0;
代码如下: