背景
组合、子集、排列问题都是经典的回溯算法,有共同的特点但是也有不一样的实现细节,这篇文章总结自己刷题过程中的经验。
总结
首先看组合问题还是排列问题
子集,组合属于组合问题。
排列属于排列问题。
如果数组中没有重复元素
子集、组合问题使用start指针,每一次搜索从start指针后的元素开始搜索,保证不要选取重复的元素。
排列问题使用visit数组,对自身进行标记,每一次搜索从头开始搜索,但是不要选取自身。
如果数组中含有重复元素
这里注意对同一层搜索进行去重,有两种方法。可以想象,回溯就是一个递归树的过程。
- 使用哈希表记录当前层有哪些元素,如果搜索当前层的过程中遇到重复元素,跳过这次搜索。
- 首先对数组进行排序,如果nums[i-1]==nums[i],如果在当前层,nums[i]已经被搜索过,跳过本次搜索
如何剪枝?
排序是剪枝的前提。
子集问题
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
backTracking(result, path, nums, 0);
return result;
}
private void backTracking(List<List<Integer>> result, LinkedList<Integer> path, int[] nums, int start) {
result.add(new LinkedList<>(path));
for (int i = start; i < nums.length; i++) {
path.add(nums[i]);
backTracking(result, path, nums, i + 1);
path.removeLast();
}
}
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
Arrays.sort(nums);
boolean[] used = new boolean[nums.length];
Arrays.fill(used, false);
dfs(nums, result, path, 0, used);
return result;
}
private void dfs(int[] nums, List<List<Integer>> result, LinkedList<Integer> path, int start, boolean[] used) {
result.add(new LinkedList<>(path));
for (int i = start; i < nums.length; i++) {
// used[i - 1] == true,说明同一树支candidates[i - 1]使用过
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
// 而我们要对同一树层使用过的元素进行跳过
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) { // 也可以写成i > start && nums[i] == nums[i - 1]
continue;
}
path.add(nums[i]);
used[i] = true;
dfs(nums, result, path, i + 1, used);
used[i] = false;
path.removeLast();
}
}
组合问题
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
backTracking(result, path, n, k, 1);
return result;
}
private void backTracking(List<List<Integer>> result, LinkedList<Integer> path, int n, int k, int index) {
if (path.size() == k) {
result.add(new LinkedList<>(path));
return;
}
for (int i = index; i <= n; i++) {
path.add(i);
backTracking(result, path, n, k, i + 1);
path.removeLast();
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
// 排序是剪枝的前提
Arrays.sort(candidates);
backTracking(candidates, result, path, 0, target); //index是确保从candidates数组的index下标开始找数字的和,避免重复出现[2,2,3][2,3,2]的情况
return result;
}
private void backTracking(int[] candidates, List<List<Integer>> result, List<Integer> path, int index, int target) {
// 结束条件
if (0 == target) {
result.add(new ArrayList<>(path));
return;
}
// 选择列表
for (int i = index; i < candidates.length; i++) {
if (candidates[i] > target) {
break;
}
path.add(candidates[i]);
// i可以重复,不是从i+1开始
backTracking(candidates, result, path, i, target - candidates[i]);
path.remove(path.size()-1);
}
}
40. 组合总和 II
含有重复元素
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new LinkedList<>();
// 剪枝与去重
Arrays.sort(candidates);
dfs(result, path, candidates, 0, target);
return result;
}
private void dfs(List<List<Integer>> result, List<Integer> path, int[] candidates, int start, int target) {
if (target == 0) {
result.add(new LinkedList<>(path));
return;
}
if (target < 0) {
return;
}
for (int i = start; i < candidates.length; i++) {
if (i > start && candidates[i] == candidates[i - 1]) {
continue;
}
// 剪枝
if (target - candidates[i] < 0) {
break;
}
path.add(candidates[i]);
dfs(result, path, candidates, i + 1, target - candidates[i]);
path.remove(path.size() - 1);
}
}
排列问题
public List<List<Integer>> permute(int[] nums) {
LinkedList<List<Integer>> result = new LinkedList<>();
final LinkedList<Integer> path = new LinkedList<>();
boolean[] visited = new boolean[nums.length];
backTrack(nums, path, result,visited);
return result;
}
private void backTrack(int[] nums, LinkedList<Integer> path, LinkedList<List<Integer>> result,boolean[] visited) {
if (path.size() == nums.length) {
result.add(new LinkedList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (visited[i]) {
continue;
}
path.add(nums[i]);
visited[i] = true;
backTrack(nums, path, result, visited);
visited[i] = false;
path.removeLast();
}
}
47. 全排列 II
含有重复元素
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new LinkedList<>();
boolean[] visited = new boolean[nums.length];
Arrays.fill(visited, false);
Arrays.sort(nums);
dfs(result, path, visited, nums);
return Collections.emptyList();
}
private void dfs(List<List<Integer>> result, List<Integer> path, boolean[] visited, int[] nums) {
if (path.size() == nums.length) {
result.add(new LinkedList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
// 已经使用过nums[i]
if (visited[i]) {
continue;
}
// 已经使用过nums[i-1] --> visit[i-1]==true nums[i-1]在路径中
// 未使用nums[i-1] --> visit[i-1]=false, nums[i-1]不在路径中
if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) {
continue;
}
visited[i] = true;
path.add(nums[i]);
dfs(result, path, visited, nums);
visited[i] = false;
path.remove(path.size() - 1);
}
}