首页 > 编程语言 > 详细

LeetCode中Valid Parentheses的JAVA实现

时间:2015-05-28 09:29:53      阅读:225      评论:0      收藏:0      [点我收藏+]

先上题目:

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) {
        
LinkedList<Character> stack = new LinkedList<Character>();

for(char c:s.toCharArray())
{
if(!stack.isEmpty())
{
if(stack.peek()==40&&c==41||stack.peek()==91&&c==93||stack.peek()==123&&c==125)
{
stack.pop();
}else
{
stack.push(c);
}
}else
{
stack.push(c);
}

}
return stack.isEmpty()?true:false;
    }
}


LeetCode中Valid Parentheses的JAVA实现

原文:http://blog.csdn.net/snchenjt/article/details/46117219

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!