oj20. Valid Parentheses
来源:互联网 发布:java模拟器 编辑:程序博客网 时间:2024/04/27 04:42
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 boolean isValid(String s) { char[] ca = s.toCharArray(); int len = ca.length; if(len%2 != 0) return false; Stack st = new Stack(); for(int i=0; i<len;i++){ if(ca[i]=='(' || ca[i]=='[' || ca[i]=='{') st.push(ca[i]); else{ if(st.empty()) return false; char temp = (char)st.pop(); if(ca[i]==')'&& temp== '(') continue; if(ca[i]==']'&& temp== '[') continue; if(ca[i]=='}'&& temp== '{') continue; return false; } } if(st.empty()) return true; else return false; }
0 0
- oj20. Valid Parentheses
- OJ20
- 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
- ZOJ 3965 Binary Tree Restoring (递归)
- SecureCRT连接虚拟机中的Linux系统(Ubuntu)
- vsftp被动模式启用iptables访问设置
- LTE issue 3
- 深度学习存在的问题及解决方法
- oj20. Valid Parentheses
- WireShark技巧、Android OOM案例分析
- spring的测试
- SQLITE3 使用总结(六)
- ORACLE中树操作递归算法connect by prior
- python中真假值的问题和and or 的串联用法
- Camtasia Studio 8.4.4破解版安装教程(图)附下载
- 3GPP TS协议系列总结
- 重置oracle的sys和system的登录密码