Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid.
输入
Input: "()"
输出
Output: true
样例
标准输入 复制文本 |
()[]{} |
标准输出 复制文本 |
true |
标准输入 复制文本 |
(] |
标准输出 复制文本 |
false |
标准输入 复制文本 |
([)] |
标准输出 复制文本 |
false |
提示
遇到左括号就进栈push,遇到右括号并且栈顶为与之对应的左括号,就把栈顶元素出栈。最后看栈⾥⾯还有没有其他元素,如果为空,即匹配。 需要注意,空字符串是满⾜括号匹配的,即输出 true。