share my 0ms C++ solution


  • 0
    Z
    class Solution {
    public:
        vector<string> generateParenthesis(int n) {
            num = n;
            vector<string> res;
            string s = "(";
            add(res,s,1,0);
            return res;
        }
    private:
        int num;
        void add(vector<string> &res,string s,int left,int right)
        {
            if(left != num || right != num)
            {
                string ss(s);
                if(right != num && left > right)
                {
                    s += ")";
                    add(res,s,left,right+1);
                }
                if(left != num)
                {
                    ss += "(";
                    add(res,ss,left+1,right);
                }
            }
            else 
                res.push_back(s);
        }
    };

Log in to reply
 

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