22. Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()" 

class Solution {
public:
    vector<string>v;
    void generate(string s,int l,int r){
        if(l==0&r==0)v.push_back(s);
        if(l>0)generate(s+'(',l-1,r);
        if(l<r&&r>0)generate(s+')',l,r-1);
    }
    vector<string> generateParenthesis(int n) {
        generate("",n,n);
        return v;
    }
};


你可能感兴趣的:(22. Generate Parentheses)