20. Valid Parentheses
来源:互联网 发布:淘宝网靠枕 编辑:程序博客网 时间:2024/06/14 04:16
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.
解析:利用栈的后进先出特点
public class Solution { public boolean isValid(String s) { if (s.length() % 2 != 0) { return false; } char[] sArr = s.toCharArray(); HashMap<Character, Character> hm = new HashMap<Character, Character>(); hm.put(')','('); hm.put('}','{'); hm.put(']','['); Stack<Character> st = new Stack<Character>(); for(char c : sArr) { if (!hm.containsKey(c)) { st.push(c); } else { if (st.isEmpty() || st.pop() != hm.get(c)) { return false; } } } return st.isEmpty(); }}
0 0
- LeetCode 20. Valid Parentheses
- [LeetCode] 20.Valid Parentheses
- LeetCode --- 20. Valid Parentheses
- LeetCode 20.Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- 20.Valid Parentheses
- 20.Valid Parentheses
- 20. Valid Parentheses
- 【leetcode】20. Valid Parentheses
- [leetcode] 20.Valid Parentheses
- [leetcode] 20. Valid Parentheses
- 20. Valid Parentheses
- 20. Valid Parentheses
- 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- 20. Valid Parentheses LeetCode
- 20. Valid Parentheses
- 20. Valid Parentheses
- shell学习笔记四
- shell 常用脚本
- 使用Fiddler对IOS系统进行HTTP抓包
- 素数和哥德巴赫
- 【九度OJ】题目1185:特殊排序 解题报告
- 20. Valid Parentheses
- java只有值传递
- sed行定位的使用
- SpringMVC和Struts2.x区别
- 2024.C语言合法标识符
- Retrofit
- HTML——初识html,元素 属性 段落 文本格式化 链接 头部入门
- 并查集专题训练解题报告
- 剑指Offer系列-面试题46:求1+2+3+···+n