数字 n
代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
示例 1:
输入:n = 3 输出:["((()))","(()())","(())()","()(())","()()()"]
示例 2:
输入:n = 1 输出:["()"]
提示:
1 <= n <= 8
class Solution {
List<String> list=new ArrayList<>();
public List<String> generateParenthesis(int n) {
String temp="";
dfs(list,temp,0,0,n);
return list;
}
public void dfs(List<String> list,String str,int l,int r,int n){
if(r>l||r>n||l>n){
return;
}
if(l==n&&r==n){
list.add(str);
}
dfs(list,str+"(",l+1,r,n);
dfs(list,str+")",l,r+1,n);
return;
}
}