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> generateParenthesis(int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(n == 0) { return vector<string>(1, ""); } if(n == 1) { return vector<string>(1, "()"); } vector<string> left; vector<string> right; vector<string> svec; int i = 0, j = 0, k = 0; string str; for(k = n - 1; k >= 0; --k) { left = generateParenthesis(k); right = generateParenthesis(n - 1 - k); for(i = 0; i < left.size(); ++i) { for(j = 0; j < right.size(); ++j) { str.clear(); str.append("("); str.append(left[i]); str.append(")"); str.append(right[j]); svec.push_back(str); } } } return svec; } };这个递归有点麻烦