随想录一刷Day25——回溯算法

文章目录

  • Day25_回溯算法
    • 5. 电话号码的字母组合
    • 6. 组合总和
    • 8. 组合总和 II

Day25_回溯算法

5. 电话号码的字母组合

17. 电话号码的字母组合
思路:

按照给定的按键顺序递归回溯字母组合

class Solution {
private:
    const string dict[10] = {
        "",     // 0
        "",     // 1
        "abc",  // 2
        "def",  // 3
        "ghi",  // 4
        "jkl",  // 5
        "mno",  // 6
        "pqrs", // 7
        "tuv",  // 8
        "wxyz", // 9
    };

    string s;
    vector<string> result;

    void backtracking(const string &digits, int depth) {
        if (digits.size() == depth) {
            result.push_back(s);
            return ;
        }
        string letters = dict[digits[depth] - '0'];
        int len = letters.length();
        for (int i = 0; i < len; i++) {
            s.push_back(letters[i]);
            backtracking(digits, depth + 1);
            s.pop_back();
        }
    }

public:
    vector<string> letterCombinations(string digits) {
        s.clear();
        result.clear();
        if (digits.size() == 0) return result;
        backtracking(digits, 0);
        return result;
    }
};

6. 组合总和

39. 组合总和
思路:

常规回溯,此处可以重复选择

class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
    int index;
    void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {
        if (sum == target) {
            result.push_back(path);
            return ;
        }
        if (sum > target) {
            return ;
        }

        int size = candidates.size();
        for (int i = startIndex; i < size; i++) {
            path.push_back(candidates[i]);
            backtracking(candidates, target, sum + candidates[i], i); // 不是 i + 1,表示可以重复
            path.pop_back();
        }
    }

public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        path.clear();
        result.clear();
        index = 0;
        backtracking(candidates, target, 0, 0);
        return result;
    }
};

剪枝

首先对整数数组排序,当 sum 大于 target 时,后面的数如果被选中一定会大于 sum ,所以此时可以直接结束 for 循环

class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
    int index;
    void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {
        if (sum == target) {
            result.push_back(path);
            return ;
        }

        int size = candidates.size();
        for (int i = startIndex; i < size && sum + candidates[i] <= target; i++) { // 剪枝
            path.push_back(candidates[i]);
            backtracking(candidates, target, sum + candidates[i], i);
            path.pop_back();
        }
    }

public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        path.clear();
        result.clear();
        index = 0;
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, 0, 0);
        return result;
    }
};

8. 组合总和 II

40. 组合总和 II
思路:

利用一个布尔数组 used 记录是否使用过某元素,去重

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) {
            result.push_back(path);
            return ;
        }
        int size = candidates.size();
        for (int i = startIndex; i < size && sum + candidates[i] <= target; i++) {
            // 如果连续两个相同的元素, 前一个用过了,再用后一个(used[i - 1] == false),此时出现重复情况
            //                          前一个和后一个同时用,(used[i - 1] == true),这种情况允许选用
            if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) continue;
            path.push_back(candidates[i]);
            used[i] = true;
            backtracking(candidates, target, sum + candidates[i], i + 1, used);
            used[i] = false;
            path.pop_back();
        }
    }

public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<bool> used(candidates.size(), false);
        result.clear();
        path.clear();
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, 0, 0, used);
        return result;
    }
};

你可能感兴趣的:(代码随想录,算法,leetcode,java)