【Leetcode笔记】40.组合总和II

1. 题目要求

在这里插入图片描述
这道题目和39.组合总和不一样的地方在于:数组中含有相同的元素。同样地,结果不能含有重复组合。
拿第一个示例来看,

candidates = [1, 1, 2, 5, 6, 7, 10]

问题在于:第一个path=[1(index = 0), 2],绝不能出现另一个path=[1(index = 1), 2]!
如何去重?
卡哥使用了两种办法,第一种引入了used数组,在深度搜索中将上一个使用过的元素的used位置置一(即used[i]==true),这样当出现 candidates[i] == candidates[i - 1] 并且 used[i - 1] == false 就可以说明上面提到的第一个path已经完成;第二种办法是直接使用for循环的起始条件startIndex是否小于i,如果小于的话,就说明上一条路径的深度搜索完成了,我们此时在同一数层上的其他元素上重新开始搜索,此时可以跳过循环。

2. ACM模式代码实现

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& candidates, int target, int sum, int startIndex, vector<bool>& used) {
        if (sum > target) return;
        if (sum == target) {
            result.push_back(path);
            return;
        }
        for (int i = startIndex; i < candidates.size(); i++) {
            if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
                continue;
            }
            sum += candidates[i];
            path.push_back(candidates[i]);
            used[i] = true;
            backtracking(candidates, target, sum, i + 1, used);
            sum -= candidates[i];
            path.pop_back();
            used[i] = false;
        }
    }
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        result.clear();
        path.clear();
        vector<bool> used(candidates.size(), false);
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, 0, 0, used);
        return result;
    }
};

int main() {
    Solution solution;
    vector<int> candidates = {10,1,2,7,6,1,5};
    int target = 8;
    vector<vector<int>> result = solution.combinationSum2(candidates, target);
    for (auto& combination : result) {
        for (int num : combination) {
            cout << num << " ";
        }
        cout << endl;
    }
    return 0;
}

/* 不使用used数组进行去重,可以把for循环改成如下。 */
for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) 
{
    if (i > startIndex && candidates[i] == candidates[i - 1]) 
    {
        continue;
    }

相关推荐

  1. leetcode 40. 组合总和 II

    2024-06-07 06:50:02       60 阅读
  2. LeetCode40. 组合总和 II

    2024-06-07 06:50:02       61 阅读
  3. leetcode_40.组合总和 II

    2024-06-07 06:50:02       33 阅读
  4. leetcode 40. 组合总和 II

    2024-06-07 06:50:02       46 阅读
  5. 40. 组合总和 II

    2024-06-07 06:50:02       58 阅读

最近更新

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

    2024-06-07 06:50:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 06:50:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 06:50:02       87 阅读
  4. Python语言-面向对象

    2024-06-07 06:50:02       96 阅读

热门阅读

  1. MVC前端怎么写:深入解析与实战指南

    2024-06-07 06:50:02       26 阅读
  2. 【SCSS】use的详细使用规则

    2024-06-07 06:50:02       32 阅读
  3. AR编程入门:解锁虚拟与现实交融的新世界

    2024-06-07 06:50:02       35 阅读
  4. spring boot 之 整合 knife4j 在线接口文档

    2024-06-07 06:50:02       25 阅读
  5. Sass混合宏(Mixins)使用

    2024-06-07 06:50:02       33 阅读
  6. Android中ANR的分析和解决

    2024-06-07 06:50:02       25 阅读
  7. 人机验证问题库

    2024-06-07 06:50:02       28 阅读
  8. Pytorch语义分割(2)--------模型搭建

    2024-06-07 06:50:02       33 阅读
  9. ChatGPT-3

    2024-06-07 06:50:02       33 阅读