从数组生成排列

时间:2014-10-11 12:52:25

标签: php arrays algorithm

我尝试将一个从answer生成排列的函数移植到PHP,我发现了以下内容:

function recurse($s, $arrs, $k) {
    if ($k === count($arrs)) {
        echo $s.' ';
    } else {
        foreach ($arrs[$k] as $o) {
            recurse($s.$o, $arrs, $k + 1);
        }
    }
}

给了我正确的输出

137 138 147 148 237 238 247 248

现在我希望将输出作为数组而不是字符串,但在编辑后由于某种原因我得到错误的结果:

function generatePermutations($s, $arrs, $k) {
    if ($k === count($arrs)) {
        print_r($s);
    } else {
        foreach ($arrs[$k] as $o) {
            $s[] = $o;
            generatePermutations($s, $arrs, $k + 1);
        }
    }
}

输出:

Array
(
    [0] => 1
    [1] => 3
    [2] => 7
)
Array
(
    [0] => 1
    [1] => 3
    [2] => 7
    [3] => 8
)
Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 7
)
Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 7
    [4] => 8
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 7
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 7
    [4] => 8
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 7
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 7
    [5] => 8
)

这是两个功能的输入

$in = array( array(1, 2), array(3, 4), array(7, 8) );
recurse("", $in, 0);
generatePermutations(array(), $in, 0);

我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果你使用$ s [] = $ o;在递归函数中,它将为每个数字切割数组。 您可以添加"结果"参数,它将结果集保存为数组。 请注意&标志。没有它,参数不可写",只有"可读"。

$in = array( array(1, 2), array(3, 4), array(7, 8) );
$result = array();
generatePermutations("", $in, 0, $result);
print_r($result);

function generatePermutations($s, $arrs, $k, &$result) {
    if ($k === count($arrs)) {
        $result[] = $s; 
    } else {
        foreach ($arrs[$k] as $o) {
            generatePermutations($s.$o, $arrs, $k + 1, $result);
        }   
    }   
}

输出:

Array
(
    [0] => 137
    [1] => 138
    [2] => 147
    [3] => 148
    [4] => 237
    [5] => 238
    [6] => 247
    [7] => 248
)

编辑:修改以获得Robotex的预期输出:

$in = array( array(1, 2), array(3, 4), array(7, 8) );
$result = array();
$s = array();
generatePermutations($s, $in, 0, $result);
print_r($result);

function generatePermutations(&$s, $arrs, $k, &$result) {
    if ($k === count($arrs)) {
        array_push($result, $s);
    } else {
        foreach ($arrs[$k] as $o) {
            array_push($s, $o);
            generatePermutations($s, $arrs, $k + 1, $result);
            array_pop($s);
        }   
    }   
}

输出:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 7
        )

    [1] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 8
        )

    [2] => Array
        (
            [0] => 1
            [1] => 4
            [2] => 7
        )

    [3] => Array
        (
            [0] => 1
            [1] => 4
            [2] => 8
        )

    [4] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 7
        )

    [5] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 8
        )

    [6] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 7
        )

    [7] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 8
        )

)