Valid Parentheses
来源:互联网 发布:手机淘宝店怎样发货 编辑:程序博客网 时间:2024/05/29 14:33
题目:
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.
用哈希表来存字符之间的配对关系,然后其实就是一个进栈出栈的过程(配对就出栈),最后栈为空返回true,否则false。
public class Solution { public boolean isValid(String s) { if (s == null || s.length() == 0) { return true; } HashMap<Character,Character> hashMap = new HashMap<>(); hashMap.put(')','('); hashMap.put(']','['); hashMap.put('}','{'); Stack<Character> stack = new Stack<>(); for (int i = 0;i < s.length();i++) { if (!stack.empty()) { if (hashMap.get(s.charAt(i)) == stack.peek()) { stack.pop(); } else { stack.push(s.charAt(i)); } } else { stack.push(s.charAt(i)); } } return stack.empty()?true:false; }}
0 0
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Linux 的 时间
- android沉浸式标题栏
- Ant-wiki.com 关于我们
- 安卓通过意图使系统安装apk
- 10012---JavaScript--类型转换
- Valid Parentheses
- C - Sum It Up hdu1258
- GNU C 的一大特色就是__attribute__ 机制
- js----显示当前系统时间
- [学习]JavaScript设计模式——Revealing Module(揭示模块)模式
- 10、osg::AnimationPath,osg::NodeVisitor类
- 实战c++中的string系列--string的遍历(使用下标还是iterator)
- 搭建iOS框架
- using an empty LLDB target which can cause slow memory reads from remote devices.