本文主要是介绍Backtracking,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需要注意的点:
- 是否需要排序,一般有重复需要排序;
- isVisited 可以用来避免重复选择,比如permutation
- lastVisited可以用在有重复数字的时候避免结果重复
- 不同题目区分点在于,递归中的结束条件,什么时候将tempres 放入res中,如何避免重复,每次选择的范围,选择的起始点。
例题参考如下:
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 duplicatestempList.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 necessarybacktrack(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, skiptempList.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 elementstempList.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 duplicatestempList.add(nums[i]);backtrack(list, tempList, nums, remain - nums[i], i + 1);tempList.remove(tempList.size() - 1); }}
}
这篇关于Backtracking的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!