20 Valid Parentheses
2016-03-15 16:47
429 查看
Given a string containing just the characters
The brackets must close in the correct order,
判断一个括号字符串是否是有效
'(',
')',
'{',
'}',
'['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) { Stack<Integer> stk = new Stack<Integer>(); for (int i = 0; i < s.length(); ++i) { int pos = "(){}[]".indexOf(s.substring(i, i + 1)); if (pos % 2 == 1) { if (stk.isEmpty() || stk.pop() != pos - 1) return false; } else { stk.push(pos); } } return stk.isEmpty(); } }
相关文章推荐
- Oracle存在则修改,不存在插入记录
- 泛型数据容器Demo
- 关于maven下载jar包失败的问题的总结和解决
- Modernizr——为HTML5和CSS3而生!
- DB2获取月份第一天最后一天
- 关于AssetBundle、热更新、BestHttp
- listview android:cacheColorHint,android:listSelector属性作用
- python主要知识点(与Java的区别)
- Mac os x yosemite安装appium 1.2.4及运行sample成功遇到的问题
- PHP单例模式应用详解
- Pku oj 3617 Best Cow Line(贪心)
- Hibernate+JSP+Servlet应用(OpenSessionInView模式)
- more is better(并查集)
- 第二次例会~
- HDU 1233 还是畅通工程
- 服务端备份错误的解决方法,利用fastcopy工具备份
- 学习笔记一之transient关键字&foreach循环
- SD--RV_accounting_document_create函数的代码解读
- -bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file
- 剑指offer 包含min函数的栈