牛客题霸--括号序列题解
括号序列
http://www.nowcoder.com/questionTerminal/37548e94a270412c8b9fb85643c8ccc2
判断当前字符和下一个字符是否匹配, 把不匹配的存入栈中, 判断栈是否为空
class Solution {
public:
    bool isValid(string s) {
        stack<char>se;
        for ( int i = 0; i < s.size(); i++ ) {
            if ( se.size() == 0) se.push(s[i]);
            else {
                if ( se.top() == '[' && s[i] == ']') se.pop();
                else if ( se.top() == '(' && s[i] == ')') se.pop();
                else if ( se.top() == '{' && s[i] == '}') se.pop();
                else se.push(s[i]);
            }
        }
        if ( se.size() == 0 ) return true;
        return false;
    }
};
查看11道真题和解析


