标签:void amp ble load 返回 class lazy etc ==
22. 括号生成
数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
示例:
输入:n = 3
输出:[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
class Solution {
List<String> list = new ArrayList<>();
public List<String> generateParenthesis(int n) {
if(n==0) return list;
genres("", n, n);
return this.list;
}
private void genres(String curres, int l, int r){
// 如果左括号剩下的个数大于右括号的,就直接返回
if(l==0 && r==0){
this.list.add(curres);
return;
}
if(l > r)
return;
if(l > 0){
genres(curres + ‘(‘, l-1, r);
}
if(r > 0){
genres(curres + ‘)‘, l, r-1);
}
}
}
标签:void amp ble load 返回 class lazy etc ==
原文地址:https://www.cnblogs.com/athony/p/13191351.html