当前位置: 首页 > news >正文

代码随想录 算法训练 Day23:回溯算法part02

题目一

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。 

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入: candidates = [2], target = 1
输出: []
class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {// 结果列表List<List<Integer>> result = new ArrayList<>();// 对硬币面值排序(重要!为剪枝做准备)Arrays.sort(candidates);// 开始回溯搜索backtrack(candidates, target, 0, new ArrayList<>(), 0, result);return result;}private void backtrack(int[] candidates, int target, int start, List<Integer> current, int currentSum,List<List<Integer>> result) {// 当前组合总额等于目标 → 找到有效组合if (currentSum == target) {result.add(new ArrayList<>(current)); // 创建新列表保存当前组合return;}// 从给定位置开始尝试(保证唯一顺序)for (int i = start; i < candidates.length; i++) {int coin = candidates[i];// 检查加入是否会超出目标金额if (currentSum + coin > target) {break; // 排序后,后面硬币更大 → 直接终止}// 选择当前硬币current.add(coin); // 加入组合currentSum += coin; // 更新总额// 重要:下一个位置从i开始(允许重复使用硬币)backtrack(candidates, target, i, current, currentSum, result);// 回溯:移除最后选择的硬币current.remove(current.size() - 1);currentSum -= coin;}}
}

题目二

给定一个数组 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]
]
class Solution {public List<List<Integer>> combinationSum2(int[] candidates, int target) {// 结果列表List<List<Integer>> result = new ArrayList<>();// 对硬币面值排序(重要!为剪枝做准备)Arrays.sort(candidates);backtrack(candidates, target, 0, new ArrayList<>(), 0, result);return result;}private void backtrack(int[] candidates, int target, int start, List<Integer> current, int currentSum,List<List<Integer>> result) {// 当前组合总额等于目标 → 找到有效组合if (currentSum == target) {result.add(new ArrayList<>(current)); // 创建新列表保存当前组合return;}// 从给定位置开始尝试(保证唯一顺序)for (int i = start; i < candidates.length; i++) {// 关键:跳过同一层级的重复元素if (i > start && candidates[i] == candidates[i-1]) {continue;}int coin = candidates[i];// 检查加入是否会超出目标金额if (currentSum + coin > target) {break; // 排序后,后面硬币更大 → 直接终止}// 选择当前硬币current.add(coin); // 加入组合currentSum += coin; // 更新总额// 重要:下一个位置从i开始(允许重复使用硬币)backtrack(candidates, target, i+1, current, currentSum, result);// 回溯:移除最后选择的硬币current.remove(current.size() - 1);currentSum -= coin;}}
}

题目三

先缺着

http://www.lqws.cn/news/156493.html

相关文章:

  • 玩转Docker | 使用Docker部署cashbook记账本
  • 命名管道实现本地通信
  • 知识图谱:为什么说它是AI突破认知瓶颈的最后一块拼图?
  • 云原生 DevOps 实践路线:构建敏捷、高效、可观测的交付体系
  • 口语考试准备part1(西电)
  • 接IT方案编写(PPT/WORD)、业务架构设计、投标任务
  • 视觉前沿算法复现环境配置1——2025CVPR风格迁移网络SaMam
  • 【MATLAB去噪算法】基于CEEMDAN联合小波阈值去噪算法(第四期)
  • Socket编程UDP\TCP
  • 从理论崩塌到新路径:捷克科学院APL Photonics论文重构涡旋光技术边界
  • vue-router路由问题:可以通过$router.push()跳转,但刷新后又变成空白页面
  • 【Java Web】9.Maven高级
  • 【opencv】基础知识到进阶(更新中)
  • 老项目的xtp1.19升级否
  • 开疆智能Etherenet转Modbus网关连接欧姆龙PLC配置案例
  • 为什么需要自动下载浏览器驱动?
  • 如何实现ModbusRTU转ProfibusDP网关与三菱PLC的完美通讯!
  • 广东餐饮服务初级证值得考吗?
  • 【Python训练营打卡】day44 @浙大疏锦行
  • C#、VB.net——如何设置窗体应用程序的外边框不可拉伸
  • dvwa10——XSS(DOM)
  • 使用 Preetham 天空模型与硬边太阳圆盘实现真实感天空渲染
  • 【iOS】cache_t分析
  • 益莱储参加 Keysight World 2025,助力科技加速创新
  • C# ExcelWorksheet 贴图
  • 一些实用的chrome扩展0x01
  • C及C++编译链接过程详解
  • Devops系列---python基础篇二
  • LSTM-XGBoost多变量时序预测(Matlab完整源码和数据)
  • 【Go】3、Go语言进阶与依赖管理