My 0ms code using C++,using O(n) Space


  • 0
    W
    class Solution {
    private:
        vector<string> result;
        string temp;
        void insert(int left, int right){ //"left" and "right" mean the unused '(' and ')' nums respectively.
           if(left==0&&right==0){
            result.push_back(temp);
            return;
           }
           if(left>0){     
               temp.push_back('(');
               insert(left-1,right);
               temp.pop_back();
           }
           if(left<right){
               temp.push_back(')');
               insert(left,right-1);
               temp.pop_back();
           }
           
        }
    public:
        vector<string> generateParenthesis(int n) {
            if(n<=0)
                return vector<string>();
            temp = "";
            insert(n,n);
            return result;
        }
    };
    

    my 0ms code, Using O(n)Space.


Log in to reply
 

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