content
code
class Solution {
public:
vector<string> ans;
vector<string> generateParenthesis(int n) {
dfs(n, 0, 0, "");
return ans;
}
void dfs(int n, int lc, int rc, string sec)
{
if (lc == n && rc == n) ans.push_back(sec);
else
{
// ( 的条件是 l_cnt < n 就可以加
// ) 的条件是 r_cnt < n && r_cnt < l_cnt 即可合法添加)
if (lc < n ) dfs(n, lc + 1, rc, sec + '(');
if (rc < n && rc < lc) dfs(n, lc, rc + 1, sec + ')');
}
}
};