0ms C++ solution, easy to understand


  • 0
    M
    bool isValid(string s) {
        std::stack<char> bra;
        for(auto c : s)
        {
            switch(c)
            {
            case '(':
            case '[':
            case '{': bra.push(c); break;
            case ')':
                if (bra.empty() || bra.top() != '(') return false;
                else bra.pop(); 
                break;
            case ']':
                if (bra.empty() || bra.top() != '[') return false;
                else bra.pop(); 
                break;
            case '}':
                if (bra.empty() || bra.top() != '{') return false;
                else bra.pop(); 
                break;
            default :throw;
            }
        }
        return bra.empty();
    }

Log in to reply
 

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