classSolution{ public List<List<Integer>> permute(int[] nums) { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); dfs(nums, res, 0); return res; }
privatevoiddfs(int[]nums,List<List<Integer>> res,int index){ if (index==nums.length-1) { List<Integer> path = new ArrayList<>(); for (int i = 0; i < nums.length; i++) { path.add(nums[i]); } res.add(path); }
for (int i = index; i < nums.length; i++) { //排列树进行交换 swap(nums, index, i); dfs(nums,res,index+1); swap(nums, index, i); } }
classSolution{ public String getPermutation(int n, int k){ StringBuilder res = new StringBuilder(); StringBuilder num = new StringBuilder("123456789"); int[] f= newint[n]; f[0] = 1; for (int i = 1; i < f.length; i++) { f[i] = f[i-1]*i; } --k; for (int i = n; i >=1; --i) { int j = k/f[i-1]; k%=f[i-1]; res.append(num.charAt(j)); num.deleteCharAt(j); } return res.toString(); } }
classSolution{ List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { ArrayList<Integer>ans = new ArrayList<>(); dfs(nums,ans,0); return res; }
/** * 回溯 * @param nums * @param ans * @param n 从第n个数开始没有被访问 */ publicvoiddfs(int []nums,ArrayList<Integer>ans,int n){ res.add(new ArrayList(ans)); for (int i = n; i < nums.length; i++) { ans.add(nums[i]); dfs(nums, ans, i+1); ans.remove(ans.size()-1); } } }
classSolution{ List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); List<Integer>ans = new ArrayList<>(); dfs(nums,ans,0); return res; }
publicvoiddfs(int []nums,List<Integer>ans,int n){ res.add(new ArrayList(ans)); for (int i = n; i < nums.length; i++) { //剪枝情况 //这个判断需要理解 if (i > n && nums[i] == nums[i - 1] ) continue; ans.add(nums[i]); dfs(nums, ans, i+1); ans.remove(ans.size()-1); } } }
组合
题目描述
给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。
示例:
1 2 3 4 5 6 7 8 9 10
输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
classSolution{ List<List<Integer>> res = new LinkedList<>(); int k; int []nums; public List<List<Integer>> combine(int n, int k) { nums = newint[n]; this.k=k; for (int i = 0; i < nums.length; i++) { nums[i] = i+1; } LinkedList<Integer> path = new LinkedList<>(); dfs(path,0); return res; } voiddfs(LinkedList<Integer> path,int n){ if (path.size()==k) { res.add(new LinkedList(path)); return ; }
for (int i = n; i < nums.length; i++) { path.add(nums[i]); dfs(path,i+1); path.removeLast(); } } }
classSolution{ List<List<Integer>> output = new LinkedList(); int n; int k;
publicvoidbacktrack(int first, LinkedList<Integer> curr){ // if the combination is done if (curr.size() == k) output.add(new LinkedList(curr));
for (int i = first; i < n + 1; ++i) { // add i into the current combination curr.add(i); // use next integers to complete the combination backtrack(i + 1, curr); // backtrack curr.removeLast(); } }
public List<List<Integer>> combine(int n, int k) { this.n = n; this.k = k; backtrack(1, new LinkedList<Integer>()); return output; } }
classSolution{ List<List<Integer>> res = null; int[] candidates; public List<List<Integer>> combinationSum(int[] candidates, int target) { res = new LinkedList<>(); if (candidates!=null&&candidates.length>0) { LinkedList<Integer> choose = new LinkedList<>(); this.candidates = candidates; dfs(choose,target,0); } return res; }
classSolution{ List<List<Integer>> res = null; int[] candidates; public List<List<Integer>> combinationSum2(int[] candidates, int target) { res = new LinkedList<>(); if (candidates!=null&&candidates.length>0) { LinkedList<Integer> choose = new LinkedList<>(); Arrays.sort(candidates); this.candidates = candidates;
for (int i = index; i < candidates.length; i++) { //大于 index 这个很灵性,需要和大于 0 进行对比 //大于 index说明index本身是可以取的 if (i>index && candidates[i]==candidates[i-1]) { continue; }