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);
}
}
};
推荐阅读
-
Microsoft Visual C++ runtime error解决步骤图解
-
使用Visual Studio 2017作为Linux C++开发工具
-
javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair 解决方法总结
-
数据结构之链表中倒数第k个结点(C++/Java语言实现)
-
CLion - A cross-platform IDE for C and C++
-
C++四种强制转换
-
会声会影安装配置C++ 2008时提示错误该怎办?
-
c++代码示例(程序编程代码大全)
-
Ubuntu下安装并配置VS Code编译C++的方法
-
浅谈Python程序与C++程序的联合使用