首页 > 其他 > 详细

LeetCiode--Valid Parentheses

时间:2015-01-09 09:14:56      阅读:268      评论: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.

Solutions:

class Solution 
{
public:
    bool isValid(string s) 
    {
        stack<char> sta;
        for(int i=0; i<s.length(); i++)
        {
            if(s[i]=='(' || s[i] =='[' || s[i] == '{')
                sta.push(s[i]);
            else
            {
                if(sta.empty())
                    return false;
                char temp = sta.top();
                sta.pop();
                if(s[i] == ')' && temp == '(')
                    continue;
                else if(s[i] == ']' && temp == '[')
                    continue;
                else if(s[i] == '}' && temp == '{')
                    continue;
                else
                    return false;
            }
        }
        if(sta.empty())
            return true;
        return false;
    }
};


LeetCiode--Valid Parentheses

原文:http://blog.csdn.net/shaya118/article/details/42537673

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