leetcode数组汇总_LeetCode刷题实战40:组合总和 II

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从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 有序,还指按照一定顺序搜索结果。

552895be4a97981c41419c8a33f787f3.png

270af614c1de4162d50db824abb5b000.png

由第 39 题我们知道,数组 candidates 有序,也是 深度优先遍历 过程中实现「剪枝」的前提。 将数组先排序的思路来自于这个问题:去掉一个数组中重复的元素。很容易想到的方案是:先对数组 升序 排序,重复的元素一定不是排好序以后相同的连续数组区域的第 1 个元素。也就是说,剪枝发生在:同一层数值相同的结点第 2、3 ... 个结点,因为数值相同的第 11 个结点已经搜索出了包含了这个数值的全部结果,同一层的其它结点,候选数的个数更少,搜索出的结果一定不会比第 1个结点更多,并且是第 1个结点的子集。(说明:这段文字很拗口,大家可以结合具体例子,在纸上写写画画进行理解。) 代码如下:

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:组合总和

7ac91760598f63d9914fd9bf93880f8b.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值