LeetCode进阶之路(Valid Parentheses)
来源:互联网 发布:ubuntu划分区备份 编辑:程序博客网 时间:2024/05/22 00:08
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 static boolean isValid(String s) {HashMap<Character, Character> map = new HashMap<Character, Character>();map.put('(', ')');map.put('[', ']');map.put('{', '}'); Stack<Character> stack = new Stack<Character>(); for (int i = 0; i < s.length(); i++) {char curr = s.charAt(i); if (map.keySet().contains(curr)) {stack.push(curr);} else if (map.values().contains(curr)) {if (!stack.empty() && map.get(stack.peek()) == curr) {stack.pop();} else {return false;}}} return stack.empty();}注意其中的peek()方法: 查看栈顶对象而不移除它!
明天回家,会休息两天,就不更新了。
0 0
- LeetCode进阶之路(Valid Parentheses)
- leetcode之Valid Parentheses
- LeetCode之Valid Parentheses
- leetcode之Valid Parentheses
- LeetCode之Valid Parentheses
- 【LeetCode】之Valid Parentheses
- 【Leetcode】之Valid Parentheses
- leetcode之Valid Parentheses
- LeetCode之Valid Parentheses
- leetcode 之 Valid Parentheses
- leetcode之路020 Valid Parentheses
- leetcode 之 Longest Valid Parentheses
- LeetCode之Longest Valid Parentheses
- 【Leetcode】之Longest Valid Parentheses
- LeetCode之20---Valid Parentheses
- Leetcode之Longest Valid Parentheses
- leetcode之20. Valid Parentheses
- LeetCode进阶之路(Generate Parentheses)
- hdoj 1028 Ignatius and the Princess III <递归+状态记录优化----母函数????>
- Java时间工具类(把日期时间转换成xx秒前、xx分钟前、xx小时前...)
- mysql忘记root密码的解决办法
- CodeForces】604A - Uncowed Forces
- 图形引擎(三):添加应用程序中的各种组件
- LeetCode进阶之路(Valid Parentheses)
- java基础笔记精简版2
- MySQL数据库Filesort过程
- 7.12 有一行电文,已按下面规律译成密码……
- Effective Java(二) 对于所有对象都通用的方法
- 移动端拖拽的实现效果
- linphone-LinphoneManager.java文件分析
- JavaScript简单例子之DOM动态获取年月日下拉框
- 彻底搞懂Spring类加载(注解方式)