算法 Valid Parentheses
来源:互联网 发布:淘宝详情装修 编辑:程序博客网 时间:2024/06/05 09:01
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 class Solution {
public boolean isValid(String s) {
char[] array=s.toCharArray();
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<array.length;i++)
{
char ch=array[i];
if(ch=='{'||ch=='['||ch=='(')
{
stack.push(ch);
}
else
{
if(ch==')')
{
if(stack.empty())
{
return false;
}
if(stack.peek()=='(')
{
stack.pop();
}
else
{
return false;
}
}
else if(ch==']')
{
if(stack.empty())
{
return false;
}
if(stack.peek()=='[')
{
stack.pop();
}
else
{
return false;
}
}
else if(ch=='}')
{
if(stack.empty())
{
return false;
}
if(stack.peek()=='{')
{
stack.pop();
}
else
{
return false;
}
}
}
}
if(!stack.empty())
{
return false;
}
return true;
}
}
- 算法 Valid Parentheses
- LeetCode:Valid Parentheses算法详解
- 每日算法之十九:Valid Parentheses
- LeetCode算法题之Valid Parentheses
- 算法学习笔记[LeetCode]Valid Parentheses
- LeetCode算法题目:Longest Valid Parentheses
- 算法作业HW14:Leetcode20 Valid Parentheses
- 算法系列——Valid Parentheses
- 15算法课程 20. Valid Parentheses
- 算法练习(12):Longest Valid Parentheses
- 【LeetCode算法练习(C++)】Valid Parentheses
- 【算法】【Dynamic Programming】Longest Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- hander message 传自定义List<Object>
- CTreeCtrl变量的遍历
- 免费开源轻量级商业产品图表库
- 哈哈,CSDN又支持Windows Live Writer了
- WAS DMGR, NODE,SERVER 启动,停止顺序
- 算法 Valid Parentheses
- Project Eluer - 14
- CFile详解
- 2014 ACM-ICPC 广州ragional总结::Lightmoon::fengdalu
- LVS+Keepalived+Squid+Nginx+MySQL主从 高性能集群架构配置
- eclipse中svn插件导出maven工程配置
- bec复习计划
- 如何设计好的RESTful API?
- VC非标准资源操作