题目: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:
"((()))",
"(()())", "(())()", "()(())", "()()()"
char str[10000];
vector<string>ll;
class Solution {
public:
vector<string> generateParenthesis(int n) {
ll.clear();
hehe(0,n*2,0,0);
return ll;
}
void hehe(int i,int n,int leftnum,int rightnum)
{
if(i==n)
{
str[n]='\0';
string temp;
temp=str;
ll.push_back(temp);
return;
}
if(leftnum+1<=n/2)
{
str[i]='(';
hehe(i+1,n,leftnum+1,rightnum);
}
if(leftnum>rightnum)
{
str[i]=')';
hehe(i+1,n,leftnum,rightnum+1);
}
}
};