[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();
        }
    }
};
相关推荐
怕浪猫2 小时前
GEO 优化到底是什么?AI时代内容创作者必须懂的新技能
算法·面试·github
朝朝辞暮i4 小时前
C++ 第 23 课:class —— 开始真正进入面向对象
开发语言·c++·算法
7yewh4 小时前
SLAM 三维空间刚体运动(2)
数据结构·人工智能·机器人·嵌入式·slam
Evand J4 小时前
【MATLAB例程】三维RRT+APF避障路径规划与到达角(AOA)定位算法|三维路径优化与定位仿真例程
算法·matlab·路径规划·代码·定位·rrt·aoa
leikooo4 小时前
用餐厅和盘子理解栈和堆
java·数据结构·链表·rust
郑同学的笔记5 小时前
【c++随笔28】strcmp 和 memcmp对比
开发语言·c++
随意起个昵称5 小时前
【背包dp】输出方案路径
c++·动态规划
weixin_307779135 小时前
有限产能智能排产与动态重排智能体:从需求解构到技术实现
开发语言·人工智能·算法·架构
朝朝辞暮i6 小时前
C++ 第 27 课:智能指针 shared_ptr
开发语言·c++·算法
by209996 小时前
学会使用std::string类,并理解其内部是如何管理字符串的详细阐述(上)
c++·笔记·字符串·类和对象·string