自学内容网 自学内容网

(C++回溯01) 组合

77、组合

回溯题目三步走

1. 确定参数

2. 确定终止条件

3. for 循环横向遍历,递归纵向遍历

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(int n, int k, int startIndex) {
        if(path.size() == k) {
            result.push_back(path);
            return;
        }
        for(int i = startIndex; i <= n; i++) {
            path.push_back(i);
            backtracking(n, k, i + 1);
            path.pop_back();
        }
    }
    vector<vector<int>> combine(int n, int k) {
        backtracking(n, k, 1);
        return result;
    }
};

剪枝操作:

公式:n - (k - path.size()) + 1

k - path.size() 意思是还可选取多少元素

n - (k - path.size()) 意思是去掉可选取元素个数后的位置

n - (k - path.size()) + 1 为当前可以组合的最后一个位置

剪枝后代码:

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(int n, int k, int startIndex) {
        if(path.size() == k) {
            result.push_back(path);
            return;
        }
        for(int i = startIndex; i <= n; i++) {
            int flag = n - (k - path.size()) + 1;
            if(i > flag) {
                return;
            }
            path.push_back(i);
            backtracking(n, k, i + 1);
            path.pop_back();
        }
    }
    vector<vector<int>> combine(int n, int k) {
        backtracking(n, k, 1);
        return result;
    }
};

216、组合总和 III

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    int count = 0;
    void backtracking(int k, int n, int startIndex) {
        if(path.size() == k) {
            if(count == n)
            result.push_back(path);
            return;
        }
        for(int i = startIndex; i < 10; i++) {
            path.push_back(i);
            count += i;
            backtracking(k, n, i + 1);
            path.pop_back();
            count -= i;
        }
    }
    vector<vector<int>> combinationSum3(int k, int n) {
        backtracking(k, n, 1);
        return result;
    }
};

剪枝操作:

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    int count = 0;
    void backtracking(int k, int n, int startIndex) {
        if(path.size() == k) {
            if(count == n)
            result.push_back(path);
            return;
        }
        for(int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
            count += i;
            if(count > n) {
                count -= i;
                return;
            }
            path.push_back(i);
            backtracking(k, n, i + 1);
            path.pop_back();
            count -= i;
        }
    }
    vector<vector<int>> combinationSum3(int k, int n) {
        backtracking(k, n, 1);
        return result;
    }
};

17、电话号码的字母组合

class Solution {
private:
    string letterMap[10] = {
        "",
        "",
        "abc", // 2
        "def", // 3
        "ghi", // 4
        "jkl", // 5
        "mno", // 6
        "pqrs", // 7
        "tuv", // 8
        "wxyz", // 9
    };
public:
    vector<string> result;
    string path;
    void backtacking(string& digits, int index) {
        if(index == digits.size()) {
            result.push_back(path);
            return;
        }
        string letters = letterMap[digits[index] - '0'];
        for(int i = 0; i < letters.size(); i++) {
            path.push_back(letters[i]);
            backtacking(digits, index + 1);
            path.pop_back();
        }
    }
    vector<string> letterCombinations(string digits) {
        if(digits.size() == 0) {
            return result;
        }
        backtacking(digits, 0);
        return result;
    }
};


原文地址:https://blog.csdn.net/qq_44962949/article/details/140674900

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!