Generate Parentheses(C++)
程序员文章站
2022-03-26 20:04:01
givennpairs of parentheses, write a function to generate all combinations of well-formed parenthese...
givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.
class solution {
public:
vector generateparenthesis(int n)
{
vector ret;
findall(n,n,"",ret);
return ret;
}
void findall(int left,int right,string out,vector &ret)
{
if(left>right)
return;
if(left==0&&right==0)
return ret.push_back(out);
else
{
if(left>0)
findall(left-1,right,out+'(',ret);
if(right>0)
findall(left,right-1,out+')',ret);
}
}
};