码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode中Valid Parentheses的JAVA实现

时间:2015-05-28 09:32:29      阅读:206      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!