20. Valid Parentheses
2016-01-06 21:27
525 查看
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.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
public class Solution { public boolean isValid(String s) { LinkedList<Character> stack = new LinkedList<Character>(); for (int i = 0; i < s.length(); i++) { if (stack.isEmpty() || s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') { stack.push(s.charAt(i)); } else if(s.charAt(i) == ')' && stack.peek() == '(' ){ stack.pop(); } else if(s.charAt(i) == '}' && stack.peek() == '{' ){ stack.pop(); } else if(s.charAt(i) == ']' && stack.peek() == '[' ){ stack.pop(); } else{ return false; } } return stack.isEmpty(); } }
相关文章推荐
- 新浪IP 归属地查询
- hdoj2026首字母变大写
- 任务二:1、选择器 2、连接集中状态的顺序 3、浮动的用发和原理 4、盒模型在IE和Google等不同浏览器的区别与联系
- java@ LinkedList 学习
- 快排
- linux命令之ln
- nginx常见502问题的解决
- hyper-v 常用管理命令
- js中call与apply的用法小结
- Linux 静态库与动态库的使用
- 离线 + 位优化 - SGU 108 Self-numbers 2
- 纳什均衡
- 18. 4Sum leetcode Python 2016 new Season
- JAVA课程 一、初识JAVA
- 70. Climbing Stairs
- linux下安装nginx
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
- 【Java基础】理解ThreadLocal
- CSS实现水平|垂直居中漫谈
- RESTful架构1--架构理解