代码随想录算法训练营第二十三天|LeetCode39. 组合总和、LeetCode40.组合总和II、LeetCode131.分割回文串

前言

LeetCode39. 组合总和

讲解文章:代码随想录

视频讲解:LeetCode39.组合总和

LeetCode40.组合总和II

讲解文章:代码随想录

视频讲解:LeetCode:40.组合总和II

LeetCode131.分割回文串

讲解文章:代码随想录

视频讲解:LeetCode131.分割回文串


一、LeetCode39. 组合总和

题目链接:

39. 组合总和 - 力扣(LeetCode)

代码:

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void backtracing(vector<int>& candidates, int target, int startIndex, int acc)
    {
        if(acc > target)
        {
            return ;
        }
        if(acc == target)
        {
            result.push_back(path);
            return ;
        }
        
        for(int i = startIndex; i < candidates.size(); i++)
        {
            path.push_back(candidates[i]);
            backtracing(candidates, target, i, acc + candidates[i]);
            path.pop_back();
        }
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        result.clear();
        path.clear();
        backtracing(candidates, target, 0, 0);
        return result;      
    }
};

二、LeetCode40.组合总和II

 题目链接:

40. 组合总和 II - 力扣(LeetCode)

代码:

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;

    void backtracing(vector<int>& candidates, int target, int startIndex,
                     int acc, vector<bool>& used) {
        if (acc == target) {
            result.push_back(path);
            return;
        }
        if (acc > target) {
            return;
        }

        for (int i = startIndex; i < candidates.size(); i++) {
            if(i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false)
                continue;
            used[i] = true;
            path.push_back(candidates[i]);
            backtracing(candidates, target, i + 1, acc + candidates[i], used);
            path.pop_back();
            used[i] = false;
        }
    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        sort(candidates.begin(), candidates.end());
        vector<bool> used(candidates.size() + 1, false);
        backtracing(candidates, target, 0, 0, used);
        return result;
    }
};

三、LeetCode131.分割回文串

 题目链接:

131. 分割回文串 - 力扣(LeetCode)

代码:

class Solution {
public:
    vector<vector<string>> result;
    vector<string> path;

    bool isPastoralString(string& s, int left, int right)
    {
        while(left < right)
        {
            if(s[left] != s[right])
                return false;
            left++;
            right--;
        }
        return true;
    }

    void backtracing(string& s, int startIndex)
    {
        if(startIndex == s.size())
        {
            result.push_back(path);
            return ;
        }

        for(int i = startIndex; i < s.size(); i++)
        {
            // 如果不是回文串,就直接continue
            if(!isPastoralString(s, startIndex, i))
            {
                continue;
            }
            
            path.push_back(s.substr(startIndex, i - startIndex + 1));
            backtracing(s, i + 1);
            path.pop_back();
        }
    }

    vector<vector<string>> partition(string s) {
        backtracing(s, 0);
        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值