算法训练营day27

ops/2024/9/25 4:29:12/
一、组合总和

参考链接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/ops/32389.html

相关文章

Docker部署RabbitMQ与简单使用

官网地址&#xff1a; Messaging that just works — RabbitMQ 我的Docker博客:Docker-CSDN博客 1.结构 其中包含几个概念&#xff1a; **publisher**&#xff1a;生产者&#xff0c;也就是发送消息的一方 **consumer**&#xff1a;消费者&#xff0c;也就是消费消息的一方 …

大数据组件之Storm简介

Storm是一个开源的分布式实时计算系统&#xff0c;用于处理大规模、高速的流式数据。它可以实时地处理和分析数据流&#xff0c;并支持容错和可扩展性。 Storm的核心概念是拓扑&#xff08;Topology&#xff09;&#xff0c;一个拓扑由一个或多个处理节点&#xff08;bolts&am…

自然科学领域基于ChatGPT大模型的科研绘图

以ChatGPT、LLaMA、Gemini、DALLE、Midjourney、Stable Diffusion、星火大模型、文心一言、千问为代表AI大语言模型带来了新一波人工智能浪潮&#xff0c;可以面向科研选题、思维导图、数据清洗、统计分析、高级编程、代码调试、算法学习、论文检索、写作、翻译、润色、文献辅助…

CSS 文字超出显示滚动条

1、布局结构 <view class"tes" style"margin-top: 15rpx;"><p v-html"conten" class"conten"></p> </view> conten 里是内容 2、页面样式 .tes::-webkit-scrollbar-track-piece {background-color: rgba(…

附录F:上市,还是不上市?

< 回到目录 附录F&#xff1a;上市&#xff0c;还是不上市&#xff1f; 这是个问题。 当公司业绩良好时&#xff0c;有许多人希望你的公司上市&#xff1a;投资人、员工、顾问、朋友、家人等等。上市的股票为投资人和员工创造了最大的流动性&#xff0c;通常是最高价格。 …

【强训笔记】day9

NO.1 思路&#xff1a;利用两个string&#xff0c;一个输入数据&#xff0c;一个做逗号处理&#xff0c;如果该字符的位数减去下标减去1等于3的倍数的话&#xff0c;该位置就插入逗号。 代码实现&#xff1a; #include<iostream> #include<string> using names…

阿里低代码引擎学习记录

官网 一、关于设计器 1、从设计器入手进行低代码开发 设计器就是我们用拖拉拽的方法&#xff0c;配合少量代码进行页面或者应用开发的在线工具。 阿里官方提供了以下八个不同类型的设计器Demo&#xff1a; 综合场景Demo&#xff08;各项能力相对完整&#xff0c;使用Fusion…

环形链表的判断方法与原理证明

&#xff08;题目来源&#xff1a;力扣&#xff09; 一.判读一个链表是否是环形链表 题目&#xff1a; 解答&#xff1a; 方法&#xff1a;快慢指针法 内容&#xff1a;分别定义快慢指针&#xff08;fast和slow&#xff09;&#xff0c;快指针一次走两步&#xff0c;慢指…