【重点】【回溯】39.组合总和

题目
注意跟另外1道题目对比记忆:零钱兑换,可以认为零钱兑换是本题的一种特殊场景。

Python

法1:DFS+回溯_选或不选

# 写法1
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        res, path = [], []
        self.dfs(candidates, 0, res, path, target)

        return res

    def dfs(self, candidates, start, res, path, residual):
        if start < len(candidates) and residual == 0:
            res.append(path.copy())
            return 
        
        if start >= len(candidates) or residual < 0:
            return
        
        path.append(candidates[start])
        self.dfs(candidates, start, res, path, residual - candidates[start]) # 选择candidates[start]
        path.pop()
        self.dfs(candidates, start+1, res, path, residual) # 不选择candidates[start]


# 写法2
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        res, tmp = list(), list()
        candidates.sort()
        self.dfs(candidates, 0, target, res, tmp)

        return res
    
    def dfs(self, candidates, start, target, res, tmp):
        if target == 0:
            res.append(tmp.copy())
            return 
        if start == len(candidates) or target < candidates[start]:
            return 

        self.dfs(candidates, start+1, target, res, tmp) # 不选择当前数字
        tmp.append(candidates[start])
        self.dfs(candidates, start, target - candidates[start], res, tmp) # 选择当前数字
        tmp.pop() # 回溯: 恢复现场

        # return # 最后这里其实有个默认return, 可写可不写!

旧解法

法1:回溯

必须掌握基本算法!!!

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> tmp = new ArrayList<>();
        dfs(candidates, 0, target, res, tmp);

        return res;
    }

    public void dfs(int[] candidates, int idx, int target, List<List<Integer>> res, List<Integer> tmp) {
        if (idx == candidates.length) {
            return;
        }
        if (target == 0) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        dfs(candidates, idx + 1, target, res, tmp); // 不选idx
        if (target >= candidates[idx]) {            // 选择idx
            tmp.add(candidates[idx]);
            dfs(candidates, idx, target - candidates[idx], res, tmp);
            tmp.remove(tmp.size() - 1);
        }
    }
}

法2:回溯+剪枝

先排序,再dfs,可以进一步剪枝
参考链接

class Solution {
    void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
        // 子集和等于 target 时,记录解
        if (target == 0) {
            res.add(new ArrayList<>(state));
            return;
        }
        // 遍历所有选择
        // 剪枝二:从 start 开始遍历,避免生成重复子集
        for (int i = start; i < choices.length; i++) {
            // 剪枝一:若子集和超过 target ,则直接结束循环
            // 这是因为数组已排序,后边元素更大,子集和一定超过 target
            if (target - choices[i] < 0) {
                break;
            }
            // 尝试:做出选择,更新 target, start
            state.add(choices[i]);
            // 进行下一轮选择
            backtrack(state, target - choices[i], choices, i, res);
            // 回退:撤销选择,恢复到之前的状态
            state.remove(state.size() - 1);
        }
    }

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<Integer> state = new ArrayList<>(); // 状态(子集)
        Arrays.sort(candidates); // 对 candidates 进行排序
        int start = 0; // 遍历起始点
        List<List<Integer>> res = new ArrayList<>(); // 结果列表(子集列表)
        backtrack(state, target, candidates, start, res);
        return res;
    }
}
MATLAB代码实现了一个基于多种智能优化算法优化RBF神经网络的回归预测模型,其核心是通过智能优化算法自动寻找最优的RBF扩展参数(spread),以提升预测精度。 1.主要功能 多算法优化RBF网络:使用多种智能优化算法优化RBF神经网络的核心参数spread。 回归预测:对输入特征进行回归预测,适用于连续值输出问题。 性能对比:对比不同优化算法在训练集和测试集上的预测性能,绘制适应度曲线、预测对比图、误差指标柱状图等。 2.算法步骤 数据准备:导入数据,随机打乱,划分训练集和测试集(默认7:3)。 数据归一化:使用mapminmax将输入和输出归一化到[0,1]区间。 标准RBF建模:使用固定spread=100建立基准RBF模型。 智能优化循环: 调用优化算法(从指定文件夹中读取算法文件)优化spread参数。 使用优化后的spread重新训练RBF网络。 评估预测结果,保存性能指标。 结果可视化: 绘制适应度曲线、训练集/测试集预测对比图。 绘制误差指标(MAE、RMSE、MAPE、MBE)柱状图。 十种智能优化算法分别是: GWO:灰狼算法 HBA:蜜獾算法 IAO:改进天鹰优化算法,改进①:Tent混沌映射种群初始化,改进②:自适应权重 MFO:飞蛾扑火算法 MPA:海洋捕食者算法 NGO:北方苍鹰算法 OOA:鱼鹰优化算法 RTH:红尾鹰算法 WOA:鲸鱼算法 ZOA:斑马算法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值