参考的题解
:链接22. 括号生成
class Solution {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtracking(n, result, 0, 0, "");
return result;
}
private void backtracking(int n, List<String> result, int left, int right, String str) {
if (right > left) {
return;
}
if (left == n && right == n) {
result.add(str);
return;
}
if (left < n) {
backtracking(n, result, left+1, right, str+"(");
}
if (right < left) {
backtracking(n, result, left, right+1, str+")");
}
}
}