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);
}
}
};
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/607391.html