0 ms c++ clean code


  • 0
    L
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> my_sta;
            for(int i=0; i<s.size(); i++){
                switch(s[i]){
                    case '(':my_sta.push(s[i]);
                    break;
                    case '{':my_sta.push(s[i]);
                    break;
                    case '[':my_sta.push(s[i]);
                    break;
                    case ')':if(my_sta.empty() || my_sta.top() != '(') return false;
                    my_sta.pop();
                    break;
                    case '}':if(my_sta.empty() || my_sta.top() != '{') return false;
                    my_sta.pop();
                    break;
                    case ']':if(my_sta.empty() || my_sta.top() != '[') return false;
                    my_sta.pop();
                    break;
                }
            }
            return my_sta.empty();
        }
    };

Log in to reply
 

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