My c++ 0ms solution


  • -1
    W

    class Solution {
    private:
    char map(char c) {
    if(c == '{')return '}';
    else if(c == '(') return ')';
    else if(c == '[')return ']';
    else return c;
    }
    public:
    bool isValid(string s) {
    stack<char> st;

        for(string::iterator it = s.begin();it != s.end();it++) {
            if(*it == '{' || *it == '(' || *it == '[')st.push(*it);
            if(*it == '}' || *it == ')' || *it == ']'){
                if(st.size() == 0 || map(st.top()) != *it) return false;
                else st.pop();
            }
        }
        
        if(st.size())return false;
        else return true;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.