算法60天:day27

回溯-组合总和

力扣链接

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& candidates,int target,int sum, int startIndex){
        //终止条件
        if(sum == target){
            result.push_back(path);
        }else if(sum > target){
            return;
        }
        for(int i = startIndex;i < candidates.size(); i++){
            path.push_back(candidates[i]);
            sum += candidates[i];
            backtracking(candidates,target,sum,i);
            path.pop_back();
            sum -= candidates[i];
        }
        return;
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        path.clear();
        result.clear();
        backtracking(candidates,target,0,0);
        return result;
    }
};

回溯-组合总和 II

力扣链接

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& candidates,int target,int sum,int startIndex,vector<bool> used){
        if(sum == target){
            result.push_back(path);
            return;
        }
        for(int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++){
            // used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
            // used[i - 1] == false,说明同一树层candidates[i - 1]使用过
            if(i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false){
                continue;
            }
            sum += candidates[i];
            path.push_back(candidates[i]);
            used[i] = true;
            backtracking(candidates,target,sum,i+1,used);
            sum -= candidates[i];
            path.pop_back();
            used[i] = false;
        }
    }
    
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        result.clear();
        path.clear();
        vector<bool> used(candidates.size(),false);
        //先将candidates排序,让其相同的元素挨在一起
        sort(candidates.begin(),candidates.end());
        backtracking(candidates,target,0,0,used);
        return result;
    }
};

回溯-分割回文串

力扣链接

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

    void backtracking(const string& s,int startIndex){
        if(startIndex >= s.size()){
            result.push_back(path);
            return;
        }
        for(int i = startIndex; i < s.size(); i++){
            if(isPalindrome(s,startIndex,i)){   // 是回文子串
                // 获取[startIndex,i]在s中的子串
                string str = s.substr(startIndex,i - startIndex + 1);
                path.push_back(str);
            }else{
                continue;   // 不是回文,跳过
            }
            backtracking(s,i + 1);
            path.pop_back();
        }
    }

    bool isPalindrome(const string& s,int start,int end){
        for(int i = start,j = end; i < j;i++,j--){
            if(s[i] != s[j]){
                return false;
            }
        }
        return true;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值