s4553711
10/28/2017 - 2:13 PM

20.cpp

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