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> res;
vector<string> generateParenthesis(int n) {
res.clear();
solve(n,0,0,"");
return res;
}
void solve(int total,int leftNum,int rightNum,string s)
{
if(leftNum==total)
{
for(int i=0;i<total-rightNum;i++)
{
s+=")";
}
res.push_back(s);
return ;
}
if(leftNum>rightNum)
solve(total,leftNum,rightNum+1,s+")");
solve(total,leftNum+1,rightNum,s+"(");
}
};