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) {
vector<string> res; string temp;
if (n > 0)
{
getStr(res,temp, n, 0, 0);
}
return res;
}
void getStr(vector<string> &res,string temp, int n, int left, int right)
{
if (left > n || right > n || left < right) {
return;
}
if (left == n && right == n) {
res.push_back(temp);
return;
}
getStr(res, temp + "(", n, left + 1, right);
getStr(res, temp + ")", n, left, right + 1);
}
};