建设银行企业官方网站,网站备案被删除,中牟网络推广公司,电子商务安全问题 网站权限题目链接https://leetcode.cn/problems/IDBivT/description/
正整数 n 代表生成括号的对数#xff0c;请设计一个函数#xff0c;用于能够生成所有可能的并且 有效的 括号组合。 class Solution {public ListString generateParenthesis(int n) {ListString…题目链接https://leetcode.cn/problems/IDBivT/description/
正整数 n 代表生成括号的对数请设计一个函数用于能够生成所有可能的并且 有效的 括号组合。 class Solution {public ListString generateParenthesis(int n) {ListString res new ArrayList();String s ;dfs(0,0,n,res,s);return res;}public void dfs(int left,int right,int n,ListString res,String s){//left记录左括号数right记录右括号数。if(right left || left n){//失败的情况return;}if(right n left n){//左右括号数目都是n时添加到resres.add(s);return;}dfs(left1,right,n,res,s ( );dfs(left,right1,n,res,s ) );}
}