算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
今天和大家聊的问题叫做 组合总和 II,我们先来看题面:
https://leetcode-cn.com/problems/combination-sum-ii/
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
Each number in candidates may only be used once in the combination.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
题意
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用一次。 说明: 所有数字(包括目标数)都是正整数。 解集不能包含重复的组合。样例
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]
题解
回溯算法 + 剪枝
本题解答作者: liweiwei1419 https://leetcode-cn.com/problems/combination-sum-ii/solution/hui-su-suan-fa-jian-zhi-python-dai-ma-java-dai-m-3/这道题与上一问的区别在于: 第 39 题:candidates 中的数字可以无限制重复被选取; 第 40 题:candidates 中的每个数字在每个组合中只能使用一次。 相同点是:相同数字列表的不同排列视为一个结果。 如何去掉重复的集合(重点) 为了使得解集不包含重复的组合。有以下 2 种方案:
使用 哈希表 天然的去重功能,但是编码相对复杂;
这里我们使用和第 39 题和第 15 题(三数之和)类似的思路:不重复就需要按 顺序 搜索, 在搜索的过程中检测分支是否会出现重复结果 。注意:这里的顺序不仅仅指数组 candidates 有序,还指按照一定顺序搜索结果。
import java.util.ArrayDeque;import java.util.ArrayList;import java.util.Arrays;import java.util.Deque;import java.util.List;public class Solution {public List> combinationSum2(int[] candidates, int target) {int len = candidates.length;
List> res = new ArrayList<>();if (len == 0) {return res;
}// 关键步骤
Arrays.sort(candidates);
Deque path = new ArrayDeque<>(len);
dfs(candidates, len, 0, target, path, res);return res;
}/**
* @param candidates 候选数组
* @param len 冗余变量
* @param begin 从候选数组的 begin 位置开始搜索
* @param target 表示剩余,这个值一开始等于 target,基于题目中说明的"所有数字(包括目标数)都是正整数"这个条件
* @param path 从根结点到叶子结点的路径
* @param res
*/private void dfs(int[] candidates, int len, int begin, int target, Deque path, List> res) {if (target == 0) {
res.add(new ArrayList<>(path));return;
}for (int i = begin; i < len; i++) {// 大剪枝:减去 candidates[i] 小于 0,减去后面的 candidates[i + 1]、candidates[i + 2] 肯定也小于 0,因此用 breakif (target - candidates[i] < 0) {break;
}// 小剪枝:同一层相同数值的结点,从第 2 个开始,候选数更少,结果一定发生重复,因此跳过,用 continueif (i > begin && candidates[i] == candidates[i - 1]) {continue;
}
path.addLast(candidates[i]);// 调试语句 ①// System.out.println("递归之前 => " + path + ",剩余 = " + (target - candidates[i]));// 因为元素不可以重复使用,这里递归传递下去的是 i + 1 而不是 i
dfs(candidates, len, i + 1, target - candidates[i], path, res);
path.removeLast();// 调试语句 ②// System.out.println("递归之后 => " + path + ",剩余 = " + (target - candidates[i]));
}
}public static void main(String[] args) {int[] candidates = new int[]{10, 1, 2, 7, 6, 1, 5};int target = 8;
Solution solution = new Solution();
List> res = solution.combinationSum2(candidates, target);
System.out.println("输出 => " + res);
}
}
好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力。
上期推文:
LeetCode1-20题汇总,速度收藏!LeetCode刷题实战21:合并两个有序链表LeetCode刷题实战23:合并K个升序链表LeetCode刷题实战24:两两交换链表中的节点LeetCode刷题实战25:K 个一组翻转链表LeetCode刷题实战26:删除排序数组中的重复项LeetCode刷题实战27:移除元素LeetCode刷题实战28:实现 strStr()LeetCode刷题实战29:两数相除LeetCode刷题实战30:串联所有单词的子串LeetCode刷题实战31:下一个排列LeetCode刷题实战32:最长有效括号LeetCode刷题实战33:搜索旋转排序数组LeetCode刷题实战34:在排序数组中查找元素LeetCode刷题实战35:搜索插入位置LeetCode刷题实战36:有效的数独LeetCode刷题实战37:解数独LeetCode刷题实战38:外观数列LeetCode刷题实战39:组合总和