[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个candidayes[0]的情况,为什么是到4呢?因为4个canadidates[0]的大小已经超过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();
        }
    }
};
相关推荐
空白到白6 分钟前
决策树-面试题
算法·决策树·机器学习
flashlight_hi7 分钟前
LeetCode 分类刷题:2563. 统计公平数对的数目
python·算法·leetcode
前端世界23 分钟前
HarmonyOS 数据处理性能优化:算法 + 异步 + 分布式实战
算法·性能优化·harmonyos
YS_Geo24 分钟前
Redis 深度解析:数据结构、持久化与集群
数据结构·数据库·redis
楼田莉子25 分钟前
C++算法专题学习:栈相关的算法
开发语言·c++·算法·leetcode
njxiejing29 分钟前
Pandas数据结构(DataFrame,字典赋值)
数据结构·人工智能·pandas
tju新生代魔迷31 分钟前
数据结构:单链表以及链表题
数据结构·链表
dragoooon3432 分钟前
[数据结构——lesson3.单链表]
数据结构·c++·leetcode·学习方法
hsjkdhs33 分钟前
数据结构之链表(单向链表与双向链表)
数据结构·链表·指针
Suresoft China36 分钟前
软件测试|STATIC 代码静态验证工具 C/C++ 工具链设置指南
c++·单元测试·静态测试·测试覆盖率·static·代码覆盖率·工具链设置