代码随想录算法训练营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;
    }
};
相关推荐
熊猫_豆豆3 分钟前
YOLOP车道检测
人工智能·python·算法
艾莉丝努力练剑18 分钟前
【Linux:文件】Ext系列文件系统(初阶)
大数据·linux·运维·服务器·c++·人工智能·算法
偷吃的耗子1 小时前
【CNN算法理解】:CNN平移不变性详解:数学原理与实例
人工智能·算法·cnn
dazzle2 小时前
机器学习算法原理与实践-入门(三):使用数学方法实现KNN
人工智能·算法·机器学习
那个村的李富贵2 小时前
智能炼金术:CANN加速的新材料AI设计系统
人工智能·算法·aigc·cann
张张努力变强2 小时前
C++ STL string 类:常用接口 + auto + 范围 for全攻略,字符串操作效率拉满
开发语言·数据结构·c++·算法·stl
万岳科技系统开发2 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
张登杰踩2 小时前
MCR ALS 多元曲线分辨算法详解
算法
YuTaoShao2 小时前
【LeetCode 每日一题】3634. 使数组平衡的最少移除数目——(解法一)排序+滑动窗口
算法·leetcode·排序算法
波波0072 小时前
每日一题:.NET 的 GC是如何分代工作的?
算法·.net·gc