力扣--深度优先算法/回溯算法40.组合总和 Ⅱ

这题和组合总和Ⅰ的区别在于两点:
1.同一个数不能重复选;
2.数组中可能可以有重复的数这样会导致重复选。

例如[1,2,1],target为3时,[[1,2],[2,1]],但是这样两个算是同一组和,而如果用set把重复的去了,又会导致当target为2时,只有[2],而[1,1]这个答案就忽略了。

那么该怎么办?

思路分析:

  1. 深度优先搜索 (DFS): 通过递归实现,尝试从给定的数字集合 candidates 中选择可能的数字,构建和为 target 的组合。
  2. 递归函数 dfs
    • 接收参数:candidates 为数字集合,target 为目标和,start 为当前选择的数字起始位置,nownum 为当前组合的和。
    • 遍历当前可能的数字,如果当前数字加上当前组合的和已经超过目标值 target,则跳过当前数字。
    • 避免重复选择相同的数字,如果当前数字与前一个数字相同且不是起始位置,则跳过。
    • 将当前数字加入临时结果集 path,更新当前组合的和 nownum
    • 如果当前组合的和等于目标值 target,将临时结果集加入最终结果集 result
    • 否则,继续递归生成组合,注意起始位置更新为 i+1
    • 回溯过程中,撤销选择,继续尝试其他可能的组合。
  3. 主函数:
    • 对候选数组进行排序,以确保相同的数字放在一起,方便后续处理。
    • 调用深度优先搜索函数 dfs,从起始位置 0 和当前和 0 开始生成组合。
    • 返回最终结果。
class Solution {
    // 存储最终结果的二维数组
    vector<vector<int>> result;

    // 存储当前正在生成的组合的临时结果
    vector<int> path;

    // 定义深度优先搜索函数,用于生成组合
    void dfs(vector<int>& candidates, int target, int start, int nownum) {
        // 遍历当前可能的数字
        for (int i = start; i < candidates.size(); i++) {
            // 如果当前数字加上当前组合的和已经超过目标值 target,则跳过当前数字
            if (candidates[i] + nownum > target)
                break;

            // 避免重复选择相同的数字,如果当前数字与前一个数字相同且不是起始位置,则跳过
            if (i > start && candidates[i] == candidates[i - 1])
                continue;

            // 将当前数字加入临时结果集 path
            path.push_back(candidates[i]);
            nownum += candidates[i];

            // 如果当前组合的和等于目标值 target,将临时结果集加入最终结果集 result
            if (nownum == target)
                result.push_back(path);
            else
                // 继续递归生成组合,注意起始位置更新为 i+1
                dfs(candidates, target, i + 1, nownum);

            // 回溯,撤销选择,继续尝试其他可能的组合
            nownum -= candidates[i];
            path.pop_back();
        }
        return;
    }

public:
    // 主函数,生成和为 target 的所有组合,允许重复选择相同的数字
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        // 对候选数组进行排序,方便后续处理
        sort(candidates.begin(), candidates.end());

        // 调用深度优先搜索函数 dfs,从起始位置 0 和当前和 0 开始生成组合
        dfs(candidates, target, 0, 0);

        // 返回最终结果
        return result;
    }
};

关键在于这两步:

// 对候选数组进行排序,方便后续处理 sort(candidates.begin(), candidates.end());

// 避免重复选择相同的数字,如果当前数字与前一个数字相同且不是起始位置,则跳过
            if (i > start && candidates[i] == candidates[i - 1])
                continue;

听懂掌声

相关推荐

  1. 40. 组合总和 II

    2024-03-16 01:38:05       45 阅读
  2. 组合总和2(40

    2024-03-16 01:38:05       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-16 01:38:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-16 01:38:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-16 01:38:05       87 阅读
  4. Python语言-面向对象

    2024-03-16 01:38:05       96 阅读

热门阅读

  1. R在直方图上添加一个更平滑的密度曲线

    2024-03-16 01:38:05       39 阅读
  2. JVM配置调优

    2024-03-16 01:38:05       45 阅读
  3. C语言程序设计(第四版)—习题11程序设计题

    2024-03-16 01:38:05       38 阅读
  4. PID、LQR、MPC三者的原理及区别

    2024-03-16 01:38:05       91 阅读
  5. mybatis转义字符

    2024-03-16 01:38:05       45 阅读
  6. 563: String(python)

    2024-03-16 01:38:05       45 阅读
  7. Solidity Uniswap V2 library contract

    2024-03-16 01:38:05       42 阅读
  8. php中register_shutdown_function 函数用法详解

    2024-03-16 01:38:05       41 阅读
  9. 不学51直接学stm32可以吗?学stm32需要哪些基础?

    2024-03-16 01:38:05       42 阅读
  10. 学习vue3第六张(vue3 中 computed watch watchEffect)

    2024-03-16 01:38:05       33 阅读
  11. C++超详细知识点(五):类的友元函数和友元类

    2024-03-16 01:38:05       42 阅读