• 回溯-数组总和II


    在这里插入图片描述

    class Solution {
    private:
        vector<vector<int>> res;
        vector<int> path;
    
        int Sum(vector<int> path) {
            int sum = 0;
            for (int i = 0; i < path.size(); i++) {
                sum += path[i];
            }
            return sum;
        }
    
        void backTracking(vector<int> c, int t, int index, vector<bool> used) {
            if (Sum(path) == t) {
                res.push_back(path);
                return;
            }
            if (Sum(path) > t) {
                return;
            }
            for (int i = index; i < c.size(); i++) {
                if (i > 0 && c[i] == c[i - 1] && used[i - 1] == false) {
                    continue;
                }
                path.push_back(c[i]);
                used[i] = true;
                backTracking(c, t, i + 1, used);
                path.pop_back();
                used[i] = false;
            }
        }
    
    public:
        vector<vector<int>> combinationSum2(vector<int>& c, int t) {
            vector<bool> used(c.size(), false);
            sort(c.begin(), c.end());
            backTracking(c, t, 0, used);
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    改进后时间复杂度降低的方法:

    class Solution {
    private:
        vector<vector<int>> res;
        vector<int> path;
    
        void backTracking(vector<int>& candidates, int target, int index, int sum, vector<bool> used) {
            if (sum == target) {
                res.push_back(path);
            }
            for (int i = index; i < candidates.size() && sum + candidates[i] <= target; i++) {
                if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
                    continue;
                }
                path.push_back(candidates[i]);
                sum += candidates[i];
                used[i] = true;
                backTracking(candidates, target, i + 1, sum, used);
                path.pop_back();
                sum -= candidates[i];
                used[i] = false;
            }
        }
    
    public:
        vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
            vector<bool> used(candidates.size(), false);
            sort(candidates.begin(), candidates.end());
            backTracking(candidates, target, 0, 0, used);
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    重点:used数组很重要,确定了在for循环中,也就是树的横向遍历中可以保证可以知道这个元素是否用过,当然前提是对该数组进行了排序,如果这个元素和前一个元素数值相同,并且used[i - 1]是false,则代表前一个元素在同一树层被使用过,如果used[i - 1] 是true,则代表前一个元素在同一个数枝被使用过,而在这个题目中在同一个数枝使用相同元素是允许的,而同一树层不允许,同一树层,代表出现了相同的组合,同一数枝,代表一个组合出现相同元素。
    要注意到底是同一树层允许出现相同元素,还是同一数枝允许出现相同元素。
    同一树层能出现能出现相同元素,表示数组中的元素可以重复使用。
    同一数枝可以出现同一元素,表示数组中有相同的元素,但是要避免结果出现相同的组合。

  • 相关阅读:
    使用 Docker 部署 Answer 问答平台
    【C++】AVL树的简单实现
    LeetCode中等题之旋转图像
    AI的Prompt是什么
    ArrayList知识点(面试)
    嵌入式开发人员使用基于组件的固件的5个理由
    华为悦盒EC6108V9 、EC6108V9C_1080UI_非高安版_鸿蒙动画_免拆卡刷固件包
    angular项目启动报错
    【记录】数据透视表
    手机短信接收验证码的实现原理
  • 原文地址:https://blog.csdn.net/qq_52025040/article/details/126734434