题解 | #牛圈围栏问题#
牛圈围栏问题
https://www.nowcoder.com/practice/4e3bb97bbc2b4382a745abe953f44aee
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return string字符串vector
*/
vector<string> ans;
void dfs(string path, int left, int right) {
if (left < 0 || right < 0) return;
if (left == 0 && right == 0) {
ans.push_back(path);
return;
}
if (left > right) return;
dfs(path+"(", left-1, right);
dfs(path+")", left, right-1);
}
vector<string> generateParenthesis(int n) {
// write code here
dfs("", n, n);
return ans;
}
};
