[LeetCode] 39. 组合总和

题目描述:

给你一个 无重复元素 的整数数组 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
输出: []

提示:

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • candidates 的所有元素 互不相同
  • 1 <= target <= 40

题目链接:. - 力扣(LeetCode)

解题主要思路:

解法一:

暴力搜索,当sum == target时,记录结果;当sum > target 或者 pos == candidates.size()时,递归结束。

注意的是,因为题目说了 "candidates 中的 同一个 数字可以 无限制重复被选取 ",因此我们在进行dfs时,传入的pos不需要+1。

解题代码:

cpp 复制代码
class Solution {
public:
    vector<vector<int>> ret;
    vector<int> path;
    int aim;
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        aim = target;
        dfs(candidates, 0, 0);
        return ret;
    }
    void dfs(vector<int>& candidates, int pos, int sum)
    {
        // 结束条件
        if (sum == aim) {
            ret.push_back(path);
            return;
        }
        if (sum > aim || pos == candidates.size()) {
            return;
        }
        for (int i = pos; i < candidates.size(); ++i) {
            path.push_back(candidates[i]);  // 记录路径
            dfs(candidates, i, sum + candidates[i]);  // dfs
            path.pop_back();  // 回溯
        }
    }
};

解法二:

假如candidates = 2,3,6,7 target = 7,那我们就先看看pos=0时,0个、1个、2个、3个、4个candidayes0的情况,为什么是到4呢?因为4个canadidates0的大小已经超过target了。接着再以此类推下去。

cpp 复制代码
class Solution {
public:
    vector<vector<int>> ret;
    vector<int> path;
    int aim;
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        aim = target;
        dfs(candidates, 0, 0);
        return ret;
    }
    void dfs(vector<int>& candidates, int pos, int sum)
    {
        // 结束条件
        if (sum == aim) {
            ret.push_back(path);
            return;
        }
        if (sum > aim || pos == candidates.size()) {
            return;
        }
        for (int k = 0; sum + k*candidates[pos] <= aim; ++k) {
            if (k) path.push_back(candidates[pos]);  // 记录路径
            dfs(candidates, pos+1, sum + k*candidates[pos]);  // dfs
        }
        // 回溯
        for (int k = 1; sum + k*candidates[pos] <= aim; ++k) {
            path.pop_back();
        }
    }
};
相关推荐
vibecoding日记14 小时前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码
算法·求职·大模型工程师
yszaygr213816 小时前
Verilog参数化游程编码RLE模块
算法
望易17 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络21 小时前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
apocelipes1 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
HjhIron1 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩2 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹2 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术2 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc