欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Generate Parentheses(C++)

程序员文章站 2022-08-04 12:03:29
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);

}

}

};