代码随想录算法训练营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;
    }
};
相关推荐
yzx9910138 分钟前
Linux 系统中的算法技巧与性能优化
linux·算法·性能优化
全栈凯哥42 分钟前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表
全栈凯哥44 分钟前
Java详解LeetCode 热题 100(27):LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)详解
java·算法·leetcode·链表
SuperCandyXu1 小时前
leetcode2368. 受限条件下可到达节点的数目-medium
数据结构·c++·算法·leetcode
Humbunklung1 小时前
机器学习算法分类
算法·机器学习·分类
Ai多利1 小时前
深度学习登上Nature子刊!特征选择创新思路
人工智能·算法·计算机视觉·多模态·特征选择
Q8137574602 小时前
中阳视角下的资产配置趋势分析与算法支持
算法
yvestine2 小时前
自然语言处理——文本表示
人工智能·python·算法·自然语言处理·文本表示
GalaxyPokemon3 小时前
LeetCode - 148. 排序链表
linux·算法·leetcode
iceslime3 小时前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析