Simple recursive solution 0ms in C++


  • 5
    class Solution {
    private:
        void search(int left, int right, string path, vector<string>& v) {
            if(!left && !right) { v.push_back(path); return ; }
            if(left > 0) search(left-1, right, path+"(", v);
            if(left < right) search(left, right-1, path+")", v);
        }
    public:
        vector<string> generateParenthesis(int n) {
            vector<string> v;
            search(n, n, "", v);
            return v;
        }
    };
    

  • 0
    S

    @LHearen Simple!


Log in to reply
 

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