代码随想录算法训练营第二十七天|39. 组合总和、40.组合总和II、131.分割回文串

39. 组合总和

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
文档讲解:代码随想录
视频讲解:带你学透回溯算法-组合总和(对应「leetcode」力扣题目:39.组合总和)| 回溯法精讲!_哔哩哔哩_bilibili

C++代码:

class Solution {
public:
    vector path;
    vector> result;
    void backtrack(vector& candidates, int target, int index, int sum){
        if(sum == target){
            result.push_back(path);
            return;
        }
        for(int i = index; i < candidates.size() && sum + candidates[i] <= target; i++){
            sum += candidates[i];
            path.push_back(candidates[i]);
            backtrack(candidates, target, i, sum);
            path.pop_back();
            sum -= candidates[i];
        }
    }
    vector> combinationSum(vector& candidates, int target) {
        path.clear();
        result.clear();
        sort(candidates.begin(), candidates.end());
        backtrack(candidates, target, 0, 0);
        return result;
    }
};

40.组合总和II

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
文档讲解:代码随想录
视频讲解:回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II_哔哩哔哩_bilibili

C++代码:

class Solution {
public:
    vector> result;
    vector path;
    void backtrack(vector& candidates, int target, int sum, int startIndex, vector used) {
        if (sum == target) {
            result.push_back(path);
            return;
        }
        for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 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;
            backtrack(candidates, target, sum, i + 1, used);
            used[i] = false;
            sum -= candidates[i];
            path.pop_back();
        }
    }
    vector> combinationSum2(vector& candidates, int target) {
        vector used(candidates.size(), false);
        sort(candidates.begin(), candidates.end());
        backtrack(candidates, target, 0, 0, used);
        return result;
    }
};

131.分割回文串

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
文档讲解:代码随想录
视频讲解:带你学透回溯算法-分割回文串(对应力扣题目:131.分割回文串)| 回溯法精讲!_哔哩哔哩_bilibili

C++代码:

class Solution {
public:
    vector> result;
    vector path;
    void backtracking (const string& s, int startIndex) {
        if (startIndex >= s.size()) {
            result.push_back(path);
            return;
        }
        for (int i = startIndex; i < s.size(); i++) {
            if (isPalindrome(s, startIndex, i)) {
                string str = s.substr(startIndex, i - startIndex + 1);
                path.push_back(str);
            } else {
                continue;
            }
            backtracking(s, i + 1);
            path.pop_back();
        }
    }
    bool isPalindrome(const string& s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s[i] != s[j]) {
                return false;
            }
        }
        return true;
    }
    vector> partition(string s) {
        backtracking(s, 0);
        return result;
    }
};

你可能感兴趣的:(代码随想录算法训练营,算法)