机场
注意
在生成括号的过程,左括号要限制又括号数量
代码
class Solution {
private:
vector<string> result;
string str;
void backtracking(int n, int left, int right) {
if (str.size() == n * 2) {
result.push_back(str);
return;
}
if (left < n) {
str += '(';
backtracking(n, left + 1, right);
str.pop_back();
}
if (right < left) { // 这里只能传入left因为右括号小于等于左
str += ')';
backtracking(n, left, right + 1);
str.pop_back();
}
}
public:
vector<string> generateParenthesis(int n) {
backtracking(n,0,0);
return result;
}
};
标签:right,int,括号,算法,str,回溯,backtracking,left
From: https://www.cnblogs.com/tsqo/p/17186956.html