Sharing my 0ms C++ solution


  • 0
    T
    class Solution {
    public:
        bool isValid(string s) {
            vector<char> right;
            int n = s.length();
            int i;
            for(i=0; i<n; i++)
            {
                if(s[i] == '(')
                    right.push_back(')');
                else if(s[i] == '[')
                    right.push_back(']');
                else if(s[i] == '{')
                    right.push_back('}');
                else if(s[i] == ')')
                {
                    if(right.size()>0 && right.back() == ')')
                        right.pop_back();
                    else
                        return false;
                }
                else if(s[i] == ']')
                {
                    if(right.size()>0 && right.back() == ']')
                        right.pop_back();
                    else
                        return false;
                }
                else if(s[i] == '}')
                {
                    if(right.size()>0 && right.back() == '}')
                        right.pop_back();
                    else 
                        return false;
                }
                else
                    exit(1);
            }
            if(right.size() == 0)
                return true;
            else
                return false;
        }
    };

Log in to reply
 

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