购物网站开发和运行环境,普通人学python有意义吗,懒懒淘客怎么做自己的网站,免费云主机试用给出 n 代表生成括号的对数#xff0c;请你写出一个函数#xff0c;使其能够生成所有可能的并且有效的括号组合。
例如#xff0c;给出 n 3#xff0c;生成结果为#xff1a;
[ ((())), (()()), (())(), ()(())请你写出一个函数使其能够生成所有可能的并且有效的括号组合。
例如给出 n 3生成结果为
[ ((())), (()()), (())(), ()(()), ()()() ]
提交的代码
class Solution { public ListString generateParenthesis(int n) { ListString result new ArrayListString(); generate(result,,0,0,n); return result; } public void generate(ListString res,String str,int count1,int count2,int n) { if(count1n||count2n) { return; } if(count1ncount2n) { res.add(str); } if(count1count2) { //String ans new String(str); generate(res,str(,count11,count2,n); generate(res,str),count1,count21,n); } }
}