leetcode [#20] | GCidea's blog
目录

题目
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.
解决方案
1 | public class Solution { |
注意事项
- 要求三种括号能够正确匹配。
- 使用数据结构栈来实现:每当遇到一组括号的“左部分”,压入堆栈;每当遇到“右部分”,堆栈弹出一个元素,与当前的“右部分”相比,看能否匹配。