【leetcode hot 100 39】组合总和

devtools/2025/3/28 21:54:21/

错误解法一:每一次回溯都遍历提供的数组

class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> result = new ArrayList<List<Integer>>();List<Integer> temp = new ArrayList<Integer>();int sum = 0;backtrack(candidates, target, result, temp, sum);return result;}public void backtrack(int[] candidates, int target, List result, List temp, int sum){if(sum==target){result.add(temp);}for(int i=0; i<candidates.length; i++){temp.add(candidates[i]);backtrack(candidates, target, result, temp, sum-candidates[i]);temp.remove(temp.size()-1);}}
}

错误原因:

  • 没有回溯中止条件
  • 未考虑到数字相同但是位置不同的情况,我们把这种情况也算作一次结果

解法一:(回溯法)每一次回溯尝试把第idx个数加进去,考虑重复使用candidates[idx],或不使用candidates[idx](即:跳过)

class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> result = new ArrayList<List<Integer>>();List<Integer> temp = new ArrayList<Integer>();int idx = 0; // 从第0个数开始回溯backtrack(candidates, target, result, temp, idx);return result;}public void backtrack(int[] candidates, int target, List result, List temp, int idx){// temp拿到所有candidateif (idx == candidates.length) {return;}if(target==0){result.add(new ArrayList<Integer>(temp)); // 这里要new,不能直接add tempreturn;}// 选择当前数if(target-candidates[idx]>=0){temp.add(candidates[idx]);backtrack(candidates, target-candidates[idx], result, temp, idx);temp.remove(temp.size()-1);}// 不选择当前数,直接回溯backtrack(candidates, target, result, temp, idx+1);}
}

注意:

  • 这里要new,不能直接result.add(temp)result.add(new ArrayList<Integer>(temp))
  • 结束标值:
    • temp拿到所有candidate
    • 找到和为target的序列

http://www.ppmy.cn/devtools/169255.html

相关文章

使用 Selenium 控制现有 Edge 窗口以规避爬虫检测

在网络爬虫开发中&#xff0c;网站的防爬机制常常会检测自动化工具&#xff08;如 Selenium&#xff09;启动的浏览器实例。为了绕过这种检测&#xff0c;一种有效的方法是利用 Selenium 连接到手动打开的现有浏览器窗口&#xff0c;而不是每次都启动一个新的实例。本文将详细介…

python每日十题(5)

保留字&#xff0c;也称关键字&#xff0c;是指被编程语言内部定义并保留使用的标识符。Python 3.x版本中有35个保留字&#xff0c;分别为&#xff1a;and, as,assert,async,await,break,class,continue,def,del,elif,else, except, False, finally,for,from,global, if,import…

基于python+django+mysql的小区物业管理系统源码+运行步骤

该系统是基于pythondjango开发的小区物业管理系统。适用场景&#xff1a;大学生、课程作业、毕业设计。学习过程中&#xff0c;如遇问题可以在github给作者留言。主要功能有&#xff1a;业主管理、报修管理、停车管理、资产管理、小区管理、用户管理、日志管理、系统信息。源码…

GAF-CNN-DBO-LSSVM故障诊断/分类预测(Matlab)

GAF-CNN-DBO-LSSVM故障诊断/分类预测&#xff0c;附带模型研究报告&#xff08;Matlab&#xff09; 目录 GAF-CNN-DBO-LSSVM故障诊断/分类预测&#xff0c;附带模型研究报告&#xff08;Matlab&#xff09;效果一览基本描述程序设计参考资料 效果一览 基本描述 本研究提出的GA…

【构建CV图像识别系统】从传统方法到深度学习

目录 1. 图像的基本概念1.1 像素与色彩1.2 过滤与卷积 2. 图像分类与检测3. 图像特征的提取3.1 全局特征3.2 局部特征3.2.1 边缘&#xff08;Edge&#xff09;3.2.2 角点&#xff08;Corner&#xff09;3.2.3 SIFT 特征 4. 传统方法与深度学习在图像识别中的应用4.1 基于传统方…

Node.js 和 Vite 配置文件中`__dirname`

在 Node.js 和 Vite 配置文件中&#xff0c;__dirname 是一个全局变量&#xff0c;表示当前模块的目录名。具体来说&#xff1a; 1. Node.js 中的 __dirname 在 Node.js 环境中&#xff0c;__dirname 表示当前正在执行的 JavaScript 文件所在的目录的绝对路径。它是一个字符串…

Python 生成数据(绘制简单的折线图)

数据可视化 指的是通过可视化表示来探索数据&#xff0c;它与数据挖掘 紧密相关&#xff0c;而数据挖掘指的是使用代码来探索数据集的规律和关联。数据集可以是用一行代码就能表 示的小型数字列表&#xff0c;也可以是数以吉字节的数据。 绘制简单的折线图 下面来使用matplotl…

深度学习与计算机视觉方向

一、数学基础 模块具体内容应用场景示例学习资源推荐线性代数- 矩阵乘法、转置、逆矩阵 - 特征值/特征向量&#xff08;PCA降维&#xff09; - 张量&#xff08;Tensor&#xff09;基础PyTorch 张量操作、模型参数存储《线性代数应该这样学》、3Blue1Brown 视频微积分- 导数与偏…