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