算法训练营day27

server/2024/10/15 15:58:42/
一、组合总和

参考链接39. 组合总和 - 力扣(LeetCode)

前置题46. 全排列 - 力扣(LeetCode)

注意理解 used[i]数组(全排列) 和 begin变量(组合总和)的区别

因为i是由 begin赋值的,那么当这层遍历 执行 path.addLast(candidates[i])时 选不到小于 begin索引 的值

java">import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;public class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {int len = candidates.length;List<List<Integer>> res = new ArrayList<>();if (len == 0) {return res;}Deque<Integer> path = new ArrayDeque<>();dfs(candidates, 0, len, target, path, res);return res;}/*** @param candidates 候选数组* @param begin      搜索起点* @param len        冗余变量,是 candidates 里的属性,可以不传* @param target     每减去一个元素,目标值变小* @param path       从根结点到叶子结点的路径,是一个栈* @param res        结果集列表*/private void dfs(int[] candidates, int begin, int len, int target, Deque<Integer> path, List<List<Integer>> res) {// target 为负数和 0 的时候不再产生新的孩子结点if (target < 0) {return;}if (target == 0) {res.add(new ArrayList<>(path));return;}// 重点理解这里从 begin 开始搜索的语意
// 因为i是由 begin赋值的,那么当这层遍历 执行 path.addLast(candidates[i])时 选不到小于 begin索引 的值for (int i = begin; i < len; i++) {path.addLast(candidates[i]);// 注意:由于每一个元素可以重复使用,下一轮搜索的起点依然是 i,这里非常容易弄错dfs(candidates, i, len, target - candidates[i], path, res);// 状态重置path.removeLast();}}
}

什么时候使用 used 数组,什么时候使用 begin 变量?

  1. 排列问题,讲究顺序(即 [2, 2, 3] 与 [2, 3, 2] 视为不同列表时),需要记录哪些数字已经使用过,此时用 used 数组;
  2. 组合问题,不讲究顺序(即 [2, 2, 3] 与 [2, 3, 2] 视为相同列表时),需要按照某种顺序搜索,此时使用 begin 变量。
  3. 注意:具体问题应该具体分析, 理解算法的设计思想 是至关重要的,请不要死记硬背。
二、组合总和2

参考链接40. 组合总和 II - 力扣(LeetCode)

java">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<List<Integer>> combinationSum2(int[] candidates, int target) {int len = candidates.length;List<List<Integer>> res = new ArrayList<>();if (len == 0) {return res;}// 关键步骤Arrays.sort(candidates);Deque<Integer> 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<Integer> path, List<List<Integer>> 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 而不是 idfs(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<List<Integer>> res = solution.combinationSum2(candidates, target);System.out.println("输出 => " + res);}
}

http://www.ppmy.cn/server/23726.html

相关文章

Canal1--搭建Canal监听数据库变化

1.安装mysql 默认安装了mysql&#xff08;版本8.0.x&#xff09;&#xff1b; 新创建用户 -- 创建用户 用户名&#xff1a;canal 密码&#xff1a;Canal123456 create user canal% identified by Canal123456;授权 grant SELECT, REPLICATION SLAVE, REPLICATION CLIENT on…

树的层序遍历(详解)

下面以一道力扣题为例&#xff1a; 代码和解释如下&#xff1a; /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val val; }* TreeNode(…

医生个人品牌网红IP孵化打造赋能运营方案

【干货资料持续更新&#xff0c;以防走丢】 医生个人品牌网红IP孵化打造赋能运营方案 部分资料预览 资料部分是网络整理&#xff0c;仅供学习参考。 PPT可编辑&#xff08;完整资料包含以下内容&#xff09; 目录 个人IP运营方案 1. 目标设定 - 个人定位&#xff1a;根据医生…

【Linux笔记】基本指令(一)

一道残阳铺水中 半江瑟瑟半江红 目录 Linux基本指令 罗列目录内容&#xff1a;ls 指令 显示当前目录位置信息&#xff1a;pwd 指令 切换工作目录&#xff1a;cd 指令 创建文件修改时间戳&#xff1a;touch指令 创建空目录&#xff1a;mkdir指令 删除空目录&#xff1a;rmdir指…

【题解】NowCoder 除2!

题目来源&#xff1a;牛客 除2&#xff01; 题目描述&#xff1a; 给一个数组&#xff0c;一共有 n 个数。 你能进行最多 k 次操作。每次操作可以进行以下步骤&#xff1a;   选择数组中的一个偶数 ai &#xff0c;将其变成 ai / 2 。 现在你进行不超过 k 次操作后&#xf…

NFTScan | 04.22~04.28 NFT 市场热点汇总

欢迎来到由 NFT 基础设施 NFTScan 出品的 NFT 生态热点事件每周汇总。 周期&#xff1a;2024.04.22~ 2024.04.28 NFT Hot News 01/ ApeCoin DAO 发起「由 APE 代币支持的 NFT Launchpad」提案投票 4 月 22 日&#xff0c;ApeCoin DAO 社区发起「由 APE 代币支持的 NFT Launch…

Swift - 枚举

文章目录 Swift - 枚举1. 枚举的基本用法2. 关联值&#xff08;Associated Values&#xff09;3. 关联值举例4. 原始值5. 隐式原始值&#xff08;Implicitly Assigned Raw Values&#xff09;6. 递归枚举&#xff08;Recursive Enumeration&#xff09;7. MemoryLayout Swift -…

buuctf——web题目练习

1.极客大挑战2019 easysql 密码或者用户输入万能密码即可 关于万能密码的理解和原理&#xff0c;可以参考这篇BUUCTF[极客大挑战 2019] EasySQL 1_[极客大挑战 2019]easysql 1-CSDN博客 2.极客大挑战2019 have fun 题目源码 需要构造payload 网页传参可参考&#xff1a;…