【leetcode】46. Permutations 【回溯问题的通用方法】【java】
来源:互联网 发布:lol网络不稳断开连接 编辑:程序博客网 时间:2024/06/10 02:52
Given a collection of distinct numbers, return all possible permutations.
For example,[1,2,3]
have the following permutations:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
This structure might apply to many other backtracking questions, but here I am just going to demonstrate Subsets, Permutations, and Combination Sum.Subsets : https://leetcode.com/problems/subsets/public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, 0); return list;}private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){ list.add(new ArrayList<>(tempList)); for(int i = start; i < nums.length; i++){ tempList.add(nums[i]); backtrack(list, tempList, nums, i + 1); tempList.remove(tempList.size() - 1); }}Subsets II (contains duplicates) : https://leetcode.com/problems/subsets-ii/public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, 0); return list;}private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int start){ list.add(new ArrayList<>(tempList)); for(int i = start; i < nums.length; i++){ if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates tempList.add(nums[i]); backtrack(list, tempList, nums, i + 1); tempList.remove(tempList.size() - 1); }}Permutations : https://leetcode.com/problems/permutations/public List<List<Integer>> permute(int[] nums) { List<List<Integer>> list = new ArrayList<>(); // Arrays.sort(nums); // not necessary backtrack(list, new ArrayList<>(), nums); return list;}private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){ if(tempList.size() == nums.length){ list.add(new ArrayList<>(tempList)); } else{ for(int i = 0; i < nums.length; i++){ if(tempList.contains(nums[i])) continue; // element already exists, skip tempList.add(nums[i]); backtrack(list, tempList, nums); tempList.remove(tempList.size() - 1); } }}Permutations II (contains duplicates) : https://leetcode.com/problems/permutations-ii/public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, new boolean[nums.length]); return list;}private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, boolean [] used){ if(tempList.size() == nums.length){ list.add(new ArrayList<>(tempList)); } else{ for(int i = 0; i < nums.length; i++){ if(used[i] || i > 0 && nums[i] == nums[i-1] && !used[i - 1]) continue; used[i] = true; tempList.add(nums[i]); backtrack(list, tempList, nums, used); used[i] = false; tempList.remove(tempList.size() - 1); } }}Combination Sum : https://leetcode.com/problems/combination-sum/public List<List<Integer>> combinationSum(int[] nums, int target) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, target, 0); return list;}private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){ if(remain < 0) return; else if(remain == 0) list.add(new ArrayList<>(tempList)); else{ for(int i = start; i < nums.length; i++){ tempList.add(nums[i]); backtrack(list, tempList, nums, remain - nums[i], i); // not i + 1 because we can reuse same elements tempList.remove(tempList.size() - 1); } }}Combination Sum II (can't reuse same element) : https://leetcode.com/problems/combination-sum-ii/public List<List<Integer>> combinationSum2(int[] nums, int target) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, target, 0); return list; }private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){ if(remain < 0) return; else if(remain == 0) list.add(new ArrayList<>(tempList)); else{ for(int i = start; i < nums.length; i++){ if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates tempList.add(nums[i]); backtrack(list, tempList, nums, remain - nums[i], i + 1); tempList.remove(tempList.size() - 1); } }}Palindrome Partitioning : https://leetcode.com/problems/palindrome-partitioning/public List<List<String>> partition(String s) { List<List<String>> list = new ArrayList<>(); backtrack(list, new ArrayList<>(), s, 0); return list;}public void backtrack(List<List<String>> list, List<String> tempList, String s, int start){ if(start == s.length()) list.add(new ArrayList<>(tempList)); else{ for(int i = start; i < s.length(); i++){ if(isPalindrome(s, start, i)){ tempList.add(s.substring(start, i + 1)); backtrack(list, tempList, s, i + 1); tempList.remove(tempList.size() - 1); } } }}public boolean isPalindrome(String s, int low, int high){ while(low < high) if(s.charAt(low++) != s.charAt(high--)) return false; return true;}
0 0
- 【leetcode】46. Permutations 【回溯问题的通用方法】【java】
- (回溯法)LeetCode#46. Permutations
- (Java)LeetCode-46. Permutations
- [LeetCode-Java]46. Permutations
- 46.Permutations leetcode java
- 46. Permutations(回溯法)
- leetcode 46. Permutations-全排列|回溯|递归|非递归
- LeetCode 46. Permutations 全排列,树形状态回溯
- LeetCode-----46.Permutations&&47.Permutations II (全排列----回溯法)
- [LeetCode]Permutations(回溯&&DFS&&递归!!!!)
- [LeetCode]Permutations II(!!!!!DFS递归&&回溯)
- leetcode 47. Permutations II-排列|回溯算法
- LeetCode OJ-46.Permutations(全排列问题)
- leetcode 46. Permutations 全排列问题+递归
- leetcode之Permutations问题
- [leetcode]46. Permutations@Java解题报告
- [Leetcode] Permutations (Java)w
- [Leetcode] Permutations II (Java)
- 计算机系统组成与基本工作原理
- 第16周项目2 -大数据集上排序算法性能的体验
- Qt 窗口属性简介之Qt::WA_DeleteOnClose
- 阿里云服务器被黑
- 快速排序法
- 【leetcode】46. Permutations 【回溯问题的通用方法】【java】
- Python金融大数据分析-PCA分析
- 一步一步学习数据结构(4)-树和二叉树基础知识
- POJ 1942Paths on a Grid(组合数学)
- window下eclipse安装python插件
- 后缀数组(不可重叠最长重复子串)
- 设备驱动中的i2c(kernel-4.7)
- TCP协议与UDP协议的异同
- mongodb中的populate方法