PHP如何使用回溯演算法計算組合總和

醉折花枝作酒筹
發布: 2023-03-11 13:42:01
原創
1864 人瀏覽過

給定一個陣列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中文網其他相關文章!

相關標籤:
php
來源:hxd.life
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!