Problem
Given a string s
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.
Example 1:
1 | Input: s = "()" |
Example 2:
1 | Input: s = "()[]{}" |
Example 3:
1 | Input: s = "(]" |
Example 4:
1 | Input: s = "([)]" |
Example 5:
1 | Input: s = "{[]}" |
Constraints:
1 <= s.length <= 104
s
consists of parentheses only'()[]{}'
.
Analysis
这是一道很经典的stack应用题目了,利用栈的特性做括号匹配校验。遇到左边的括号就入栈,遇到右边的括号就和栈顶做匹配,一旦不符合就return false。特别需要注意两种情况:一个是遍历过程中栈已经为空而且出现右边的括号;另一个是遍历完所有后栈还有元素。这两种情况都是无法匹配的,要return false。
Solution
无
Code
1 | class Solution { |
Summary
这道题是比较经典的stack题目,这道题目其实就是算术表达式的实现原理,掌握这道题目对理解stack有很好的帮助。这道题这道题目的分享到这里,谢谢您的支持!