码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]20 Valid Parentheses

时间:2015-01-02 16:12:14      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:leetcode

https://oj.leetcode.com/problems/valid-parentheses/

http://fisherlei.blogspot.com/2013/01/leetcode-valid-parentheses.html

public class Solution {
    public boolean isValid(String s)
    {
        if (s == null)
            return true;
            
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray())
        {
            if (c == ‘(‘ || c == ‘[‘ || c == ‘{‘)
            {
                stack.push(c);
            }
            else if (c == ‘)‘)
            {
                if (stack.isEmpty() || stack.pop() != ‘(‘)
                    return false;
            }
            else if (c == ‘]‘)
            {
                if (stack.isEmpty() || stack.pop() != ‘[‘)
                    return false;
            }
            else if (c == ‘}‘)
            {
                if (stack.isEmpty() || stack.pop() != ‘{‘)
                    return false;
            }
        }
        return stack.empty();
    }
}


[LeetCode]20 Valid Parentheses

标签:leetcode

原文地址:http://7371901.blog.51cto.com/7361901/1598419

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!