Valid Parentheses

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.

Hide Tags
 Stack String
 
class Solution {
public:
    bool isValid(string s) {
        stack<char> str;
        for(int i=0;i<s.size();i++)
            if(s[i]==')' || s[i]==']' || s[i]=='}'){
                if(str.empty())
                    return false;
                else{
                    char c=str.top();
                    str.pop();
                    if(c=='('&&s[i]!=')' || c=='['&&s[i]!=']' || c=='{'&&s[i]!='}')
                        return false;
                }
            }else
                str.push(s[i]);
        return str.empty();
    }
};
原文地址:https://www.cnblogs.com/li303491/p/4092739.html