2ms c++ simple solution


  • 1
    R
    class Solution {
    public:
        bool isValid(string s) {
            vector<char> stack;
            for (int i = 0; i < s.length(); i++) {
                switch (s[i]) {
                    case '(': stack.push_back(')'); break;
                    case '[': stack.push_back(']'); break;
                    case '{': stack.push_back('}'); break;
                    default: 
                        if (stack.empty()) return false;
                        if (stack.back() != s[i]) return false;
                        stack.pop_back();
                }
            }
            return stack.empty();
        }
    };

Log in to reply
 

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