代码随想录算法训练营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;
    }
};
相关推荐
未来之窗软件服务6 小时前
自己写算法(九)网页数字动画函数——东方仙盟化神期
前端·javascript·算法·仙盟创梦ide·东方仙盟·东方仙盟算法
豐儀麟阁贵6 小时前
基本数据类型
java·算法
乐迪信息8 小时前
乐迪信息:基于AI算法的煤矿作业人员安全规范智能监测与预警系统
大数据·人工智能·算法·安全·视觉检测·推荐算法
hsjkdhs8 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法
立志成为大牛的小牛9 小时前
数据结构——十七、线索二叉树找前驱与后继(王道408)
数据结构·笔记·学习·程序人生·考研·算法
星空下的曙光9 小时前
Node.js crypto模块所有 API 详解 + 常用 API + 使用场景
算法·node.js·哈希算法
StarPrayers.10 小时前
旅行商问题(TSP)(2)(heuristics.py)(TSP 的两种贪心启发式算法实现)
前端·人工智能·python·算法·pycharm·启发式算法
爱吃橘的橘猫10 小时前
嵌入式系统与嵌入式 C 语言(2)
c语言·算法·嵌入式
2351610 小时前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
weixin_3077791312 小时前
使用Python高效读取ZIP压缩文件中的UTF-8 JSON数据到Pandas和PySpark DataFrame
开发语言·python·算法·自动化·json