class Solution {
public List<String> generateParenthesis(int n) {
ArrayList<String> res=new ArrayList<>();
if(n<=0){
return res;
}
helper(res,"",n,n);
return res;
}
private void helper(ArrayList<String> res,String s,int left,int right){
if(left>right){
return;
}
if(left==0&&right==0){
res.add(s);
return;
}
if(left>0){
helper(res,s+'(',left-1,right);
}
if(right>0){
helper(res,s+')',left,right-1);
}
}
}
LeetCode精选100题——第22题——括号生成
最新推荐文章于 2022-02-27 11:43:22 发布