LeetCode 22-Generate Parenthesis
作者:互联网
题意
给n对括号, 求所有的合法括号序列
解法
- 递归
- 递归过程中, 值传递来实现回溯
代码
vector<string> ans;
void genParenthesis(string p, int L, int R)
{
if (L < 0 || R < 0) return;
if (L == 0 && R == 0) {
ans.push_back(p);
return;
}
if (L == R)
genParenthesis(p+'(', L-1, R);
else {
genParenthesis(p+'(', L-1, R);
genParenthesis(p+')', L, R-1);
}
}
vector<string> generateParenthesis(int n) {
genParenthesis("", n, n);
return ans;
}
标签:return,22,int,Parenthesis,genParenthesis,括号,vector,ans,LeetCode 来源: https://www.cnblogs.com/Figure_at_a_Window/p/16368849.html