class Solution {//很好理解,我就不解释了
public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<String>();
if(n==0)
return res;
backtrack("",res,n,0,0);
return res;
}
public static void backtrack(String s,List<String> res,int n,int lnum,int rnum){
if(lnum==n&&rnum==n) {
res.add(s);
return;
}
if(lnum<n)
backtrack(s+"(",res,n,lnum+1,rnum);
if(lnum>rnum)
backtrack(s+")",res,n,lnum,rnum+1);
}
}
22. 括号生成
最新推荐文章于 2021-02-01 00:50:18 发布