网站做下CDN防护,云主机可以做多少网站空间,做网站图片格式,关于建筑建设的网站22. 括号生成
数字 n 代表生成括号的对数#xff0c;请你设计一个函数#xff0c;用于能够生成所有可能的并且 有效的 括号组合。 示例 1#xff1a;
输入#xff1a;n 3
输出#xff1a;[((())),(()()),(())(),()(())请你设计一个函数用于能够生成所有可能的并且 有效的 括号组合。 示例 1
输入n 3
输出[((())),(()()),(())(),()(()),()()()]示例 2
输入n 1
输出[()]提示
1 n 8
class Solution {public ListString generateParenthesis(int n) {ListString listnew ArrayList();String str;fangfa(str,n,0,0,list);return list;}public void fangfa(String str,int n,int lcount,int rcount,ListString list){if (lcountnrcountn){list.add(str);return;}if(lcountrcount||lcountn||rcountn) return;//左fangfa(str(,n,lcount1,rcount,list);//右fangfa(str),n,lcount,rcount1,list);}
}
46. 全排列
给定一个不含重复数字的数组 nums 返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。 示例 1
输入nums [1,2,3]
输出[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]示例 2
输入nums [0,1]
输出[[0,1],[1,0]]示例 3
输入nums [1]
输出[[1]]提示
1 nums.length 6-10 nums[i] 10nums 中的所有整数 互不相同
class Solution {ListListInteger listnew ArrayListListInteger();public ListListInteger permute(int[] nums) {b(0,nums);return list;}public void b(int t,int[] x){if(tx.length){ListInteger lnew ArrayList();for (int i 0; i x.length; i) {l.add(x[i]);}list.add(l);}else {for (int i t; i x.length; i) {// swap(x[t],x[i]);int tempx[t];x[t]x[i];x[i]temp;b(t1,x);// swap(x[t],x[i]);tempx[t];x[t]x[i];x[i]temp;}}}
} 无剪枝条件、递归