20. Valid Parentheses
来源:互联网 发布:java手机游戏免费下载 编辑:程序博客网 时间:2024/06/03 17:40
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) {
if(s==null||s.length()==1)
return false;
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='['){
stack.push(s.charAt(i));
}else{
if(stack.empty()){
return false;
}else{
char c = s.charAt(i);
if((c==')'&&stack.pop()=='(')||(c==']'&&stack.pop()=='[')||(c=='}'&&stack.pop()=='{')){
continue;
}else{
return false;
}
}
}
}
return stack.isEmpty();
}
- 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
- Android 禁止点击输入法的回车键并弹出提示
- Two-layer的参考点生成方法
- Android 获取TextView焦点后自动弹出软键盘
- 微信小程序怎么玩
- Leetcode 94. Binary Tree Inorder Traversal
- 20. Valid Parentheses
- Shader自学笔记 2.1.1 逐顶点漫反射光照模型实现
- Arduino+SIM900A 通过http协议实现上传数据到云服务器
- win7 64位 运行visual basic vb 速度慢的问题,解决方法
- Shader自学笔记 2.1.2 逐像素漫反射光照模型实现
- Shader自学笔记 2.1.3 半兰伯特光照模型实现
- 详解第一个CUDA程序kernel.cu
- 火星人家园科普作品受著作权法保护
- 快速幂乘 Java实现