登录  /  注册

PHP如何使用回溯算法计算组合总和

醉折花枝作酒筹
发布: 2021-07-13 15:16:59
原创
1766人浏览过

给定一个数组candidates和一个目标数target,找出candidates中所有可以使数字和为target的组合。这时候我们应该怎么做?今天小编带大家了解一下。

PHP如何使用回溯算法计算组合总和

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

所有数字(包括目标数)都是正整数。 解集不能包含重复的组合。   

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:[
 [1, 7],
 [1, 2, 5],
 [2, 6],
 [1, 1, 6]]
登录后复制

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:[  
[1,2,2],  
[5]]
登录后复制

解题思路

直接参考 回溯算法团灭排列/组合/子集问题

代码

class Solution {
    /** * @param Integer[] $candidates * @param Integer $target * @return Integer[][] */
    public $res = [];
    function combinationSum2($candidates, $target) {
        sort($candidates);   // 排序
        $this->dfs([], $candidates, $target, 0);
        return $this->res;
    }
    function dfs($array, $candidates, $target, $start) {
        if ($target < 0) return;
        if ($target === 0) {
            $this->res[] = $array;
            return;
        }
        $count = count($candidates);
        for ($i = $start; $i < $count; $i++) {
            if ($i !== $start && $candidates[$i] === $candidates[$i - 1]) continue;
            $array[] = $candidates[$i];
            $this->dfs($array, $candidates, $target - $candidates[$i], $i + 1);//数字不能重复使用,需要+1
            array_pop($array);
        }
    }}
登录后复制

额外:

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

区别是允许重复选择,在上一题基础上之改动了两处就搞定了。

class Solution {
    /** * @param Integer[] $candidates * @param Integer $target * @return Integer[][] */
    public $res = [];
    function combinationSum($candidates, $target) {
        sort($candidates);   // 排序
        $this->dfs([], $candidates, $target, 0);
        return $this->res;
    }
    function dfs($array, $candidates, $target, $start) {
        if ($target < 0) return;
        if ($target === 0) {
            $this->res[] = $array;
            return;
        }
        $count = count($candidates);
        for ($i = $start; $i < $count; $i++) {
            // if ($i !== $start && $candidates[$i] === $candidates[$i - 1]) continue; // 注释掉去重的代码
            $array[] = $candidates[$i];
            $this->dfs($array, $candidates, $target - $candidates[$i], $i);//数字能重复使用, 不需要+1
            array_pop($array);
        }
    }}
登录后复制

额外:

找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。

限制被选中方案中的元素数量

class Solution {
    public $res = [];
    /** * @param Integer $k * @param Integer $n * @return Integer[][] */
    function combinationSum3($k, $n) {
        $this->dfs([], [1,2,3,4,5,6,7,8,9], $n, 0, $k);
        return $this->res;
    }
    function dfs($array, $candidates, $n, $start, $k) {
        if ($n < 0) return;
        if ($n === 0 && count($array) === $k) {
            $this->res[] = $array;
            return;
        }
        for ($i = $start; $i < 9; $i++) {
            if ($i !== $start && $candidates[$i] === $candidates[$i - 1]) continue;
            $array[] = $candidates[$i];
            $this->dfs($array, $candidates, $n - $candidates[$i], $i + 1, $k);
            array_pop($array);
        }
    }}
登录后复制

推荐学习:php视频教程

以上就是PHP如何使用回溯算法计算组合总和的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:何晓东的博客网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号