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

LeetCiode--Valid Parentheses

时间:2015-01-09 09:16:29      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:leetcode   string   字符串   

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

标签:leetcode   string   字符串   

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

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