该代码生成匹配括号,但是把lnum-1改为--lnum时,就不正确了,
public List<String> generateParenthesis(int n) {
List<String> list = new ArrayList<>();
if(n==0)
{
return list;
}
back("", n, n, list);
return list;
}
public static void back(String s,int lnum,int rnum,List<String> list) {
if(lnum>rnum)
{
return;
}
if(lnum==0&&rnum==0)
{
list.add(s);
}else {
if(rnum>lnum)
{
back(s+")", lnum, rnum-1,list);
}
if(lnum>0){
back(s+"(", lnum-1, rnum,list);
}
}
}