当前位置: 首页 > 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.xdnf.cn/news/893413.html

相关文章:

  • 体积云完美融合GIS场景~提升视效
  • 使用 Inno 打包程序且安装 VC 运行时库
  • 人工智能100问☞第41问:什么是边缘AI?
  • RPM 数据库修复
  • 6.824 lab1
  • std::shared_ptr 与 std::unique_ptr 删除器设计差异
  • MATLAB | 绘图复刻(十九)| 轻松拿捏 Nature Communications 绘图
  • C++信号处理程序解析与改进
  • 高通LOG的使用方式
  • 城市客运安全员适合哪些人考?
  • Dagger依赖注入框架的介绍
  • Navicat-16.3.9 windows版本 MySQL客户端可视化工具 中文绿色版 无需补丁,无需破解 解压就能用
  • 江科大独立/窗口看门狗hal库实现
  • 未来教育场景下的家庭教育实训室规划:凯禾瑞华虚拟仿真技术要点前瞻
  • 不等式中的放缩法
  • 零依赖本地调试:VectorDB Lite +VectorDB CLI 高效构建向量数据库全流程
  • ceph pool 修改故障域
  • jdk-8u281-linux-x64.rpm,备用网盘下载,懒得注册官方来看看
  • 临时抱佛脚
  • 安科瑞防逆流方案落地内蒙古中高绿能光伏项目,筑牢北疆绿电安全防线
  • 基于51单片机的超声波智能避障小车仿真
  • AI开启光伏新时代:精准计算每小时发电量​
  • 单精度浮点数值 和 双精度浮点数值
  • 传统业务对接AI-AI编程框架-Rasa的业务应用实战(2)--选定Python环境 安装rasa并初始化工程
  • 高压单端探头设计中的器材应如何选型
  • 嵌入模型、问答模型以及其他常见模型类型的详细解析
  • 云服务器自带的防御可靠吗
  • this.$set() 的用法详解(Vue响应式系统相关)
  • Perforce ALM产品简介:一站式需求与测试管理平台(已通过SO 26262认证)
  • PaddleOCR(3):PaddleOCR命令讲解