My 0ms C++ solution, use a stack to store the characters.


  • 0
    D

    class Solution {
    public:

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

    };


Log in to reply
 

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