代码随想录算法训练营Day23 | 39. 组合总和 | 40.组合总和II | 131.分割回文串

今日任务

39. 组合总和

Code

cpp 复制代码
class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> ans;
        // vector<int> path;
        // int n = candidates.size();
        // function<void(int, int)> dfs = [&](int i, int t)->void{
        //     if(t == 0){
        //         ans.emplace_back(path);
        //         return;
        //     }
        //     if(i >= n || t < 0){
        //         return;
        //     }
        //     // 不选
        //     dfs(i + 1, t);

        //     path.push_back(candidates[i]);
        //     dfs(i, t - candidates[i]);
        //     path.pop_back();
        // };
        // dfs(0, target);
        // return ans;

        // ranges::sort(candidates);
        // vector<int> path;
        // int n = candidates.size();
        // function<void(int, int)> dfs = [&](int i, int t)->void{
        //     if(t == 0){
        //         ans.emplace_back(path);
        //         return;
        //     }
        //     if(i == n || t < candidates[i]){
        //         return;
        //     }
        //     dfs(i + 1, t);

        //     path.push_back(candidates[i]);
        //     dfs(i, t - candidates[i]);
        //     path.pop_back(); 
        // };
        // dfs(0, target);
        // return ans;

        ranges::sort(candidates);
        vector<int> path;
        int n = candidates.size();
        function<void(int, int)> dfs = [&](int i, int t)->void{
            if(t == 0){
                ans.emplace_back(path);
                return;
            }
            if(t < candidates[i]){
                return;
            }

            for(int j = i; j < n; j++){
                path.push_back(candidates[j]);
                dfs(j, t - candidates[j]);
                path.pop_back(); 
            }
        };
        dfs(0, target);
        return ans;
    }
};

40.组合总和II

Code

cpp 复制代码
class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>> ans;
        vector<int> path;
        ranges::sort(candidates);
        int n = candidates.size();
        function<void(int, int)> dfs = [&](int i, int t)->void{
            if(t == 0){
                ans.emplace_back(path);
                return;
            }
            if(i == n || t < candidates[i]){
                return;
            }

            // for(int j = i; j < n; j++){
            //     if(j == i || j > i && candidates[j] != candidates[j - 1]){
            //         path.push_back(candidates[j]);
            //         dfs(j + 1, t - candidates[j]);
            //         path.pop_back(); 
            //     }
            // }

        };
        dfs(0, target);
        return ans;
    }
};

131.分割回文串

Code

cpp 复制代码
class Solution {
    bool isPalindrome(string &s, int left, int right){
        while(left < right){
            if(s[left++] != s[right--]){
            return false;
            }
        }
        return true;
    }
public:
    vector<vector<string>> partition(string s) {
        vector<vector<string>> ans;
        vector<string> path;
        int n = s.size();

        // function<void(int)> dfs = [&] (int i) -> void {
        //     if(i == n){
        //         ans.emplace_back(path);
        //         return ;
        //     }

        //     for(int j = i; j < n; j++){
        //         if(isPalindrome(s, i, j)){
        //             path.push_back(s.substr(i, j - i + 1));     
        //             dfs(j + 1);
        //             path.pop_back();
        //         }
        //     }

        // };
        // dfs(0);

        // 选或不选逗号
        function<void(int, int)> dfs = [&](int i, int start)->void{
            if(i == n){
                ans.emplace_back(path);
                return;
            }

            if(i < n - 1){
                dfs(i + 1, start);
            }

            if(isPalindrome(s, start, i)){
                path.push_back(s.substr(start, i - start + 1));
                dfs(i + 1, i + 1);
                path.pop_back();
            }
        };
        dfs(0, 0);
        return ans;
    }
};
相关推荐
Ldawn_AI2 小时前
4+ 图论高级算法
算法·深度优先·图论
Xの哲學2 小时前
Linux PCI 子系统:工作原理与实现机制深度分析
linux·网络·算法·架构·边缘计算
NuyoahC3 小时前
笔试——Day46
c++·算法·笔试
Keying,,,,4 小时前
力扣hot100 | 图论 | 200. 岛屿数量、994. 腐烂的橘子、207. 课程表、208. 实现 Trie (前缀树)
算法·leetcode·图论
cwplh5 小时前
Codeforces1043 A至F 题解
算法
楼田莉子5 小时前
C++算法学习专题:滑动窗口
开发语言·数据结构·c++·学习·算法·leetcode
2501_924731115 小时前
智慧矿山误报率↓83%!陌讯多模态融合算法在矿用设备监控的落地优化
人工智能·算法·目标检测·视觉检测
zh_xuan6 小时前
LeeCode 40.组合总和II
c语言·数据结构·算法
都叫我大帅哥7 小时前
动态规划:从懵逼到装逼,一篇让你彻底搞懂DP的终极指南
java·算法
艾莉丝努力练剑8 小时前
《递归与迭代:从斐波那契到汉诺塔的算法精髓》
c语言·学习·算法