#include <vector>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return string字符串vector
*/
vector<string> ans;
vector<string> generateParenthesis(int n) {
dfs(n, 0, 0, "");
return ans;
}
void dfs(int n, int lc, int rc, string path) {
if (lc == n && rc == n) ans.push_back(path);
else {
if (lc < n) dfs(n, lc + 1, rc, path + '(');
if (rc < n && lc > rc) dfs(n, lc, rc + 1, path + ')');
}
}
};



京公网安备 11010502036488号