C++ 0ms solution


  • 0
    Y
    public:
        vector<string> generateParenthesis(int n) {
            vector<string> res;
            build(0, 0, n, "", res);
            return res;
        }
        
        void build(int left, int right, int n, string s, vector<string> &res) {
            if (s.size() == 2*n) {
                res.push_back(s);
                return;
            }
            if (left < n)
                build(left + 1, right, n, s + '(', res);
            if (left > right)
                build(left, right + 1, n, s + ')', res);
        }
    };
    

Log in to reply
 

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