//数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
//
//
//
// 示例:
//
// 输入:n = 3
//输出:[
// "((()))",
// "(()())",
// "(())()",
// "()(())",
// "()()()"
// ]
//
// Related Topics 字符串 回溯算法
// 👍 1477 👎 0
import java.util.ArrayList;
import java.util.List;
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
List<String> list = new ArrayList<>();
public List<String> generateParenthesis(int n) {
generateParenthesi(n,0,0,"");
return list;
}
public void generateParenthesi(int n,int indexl,int indexr,String s){
if (indexl == n && indexr == n){
list.add(s);
return;
}
if (indexl<n)generateParenthesi(n,indexl+1,indexr,s+"(");
if (indexl>indexr)generateParenthesi(n,indexl,indexr+1,s+")");
}
}
//leetcode submit region end(Prohibit modification and deletion)
2020-12-18 括号生成 回溯法
最新推荐文章于 2024-07-24 22:13:06 发布