public class Solution {
public List<String> generateParenthesis(int n) {
List<String> res = new LinkedList<>();
if (n < 1) {
return res;
}
helper(res, "", n, n);
return res;
}
private void helper(List<String> res, String str, int leftCount, int rightCount) {
if (leftCount == 0 && rightCount == 0) {
res.add(str);
return;
}
if (leftCount > 0) {
helper(res, str + "(", leftCount - 1, rightCount);
}
if (rightCount > 0 && rightCount > leftCount) {
helper(res, str + ")", leftCount, rightCount - 1);
}
}
}
Generate Parentheses
最新推荐文章于 2024-01-18 13:52:44 发布