-1

我有一个看起来像这样的数组: [0, 1, 2, 3, 4, 5, ...]

我需要一个函数,它会给我一个这样的数组:

[
[[0, 1, 2, 3, 4, 5]],  
[[0, 1, 2, 3, 4], [ 5 ]], 
[[0, 1, 2, 3], [ 4, 5 ]], 
[[0, 1, 2], [ 3, 4, 5 ]], 
...
[[0, 1, 2], [ 3, 4 ], [ 5 ]],
...
[[ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]]
]

当然,此输出仅适用于 6 个元素。
如果您查看输出数组的第 2、第 3 和第 4 行,它是某种组合成 2 个子数组。
如果查看输出数组的第 6 行,它会变成 3 个子数组。
在最后一行中,每个元素都应该在其自己的子数组中单独存在。

我看到了这个页面上的例子,我尝试了这些功能,但我的有点不同,因为需要尊重元素的顺序。这意味着无论括号在哪里,您都应该在每一行看到 1 2 3 4 5 6。

此外,前面提到的页面中的函数将为我提供一个包含所有子数组的数组:
[[x,x],[x],[x],[xxx]] 我不能使用它。

我需要的是这种格式:

[  
[ [ 1 , 2 , 3 ] ] ,  
[ [ 1 , 2 ] , [ 3 ] ] ,  
[ [ 1 ] , [ 2 , 3 ] ] ,  
[ [ 1 ] , [ 2 ] , [ 3 ] ]  
]

我是初学者,请有人给我提示如何做到这一点!

4

2 回答 2

0

我刚刚开发了一种非常特殊的方法来实现您正在寻找的东西(使用二进制标签遍历二叉树!我不知道它是否已经存在!!)它非常快并且不使用递归 :)

<?php
function getSpecialSubsets($in) {
    $in = array_reverse($in);
    $n = count($in);

    $out = array();
    // for every possible route
    for($i = 0; $i<pow(2, $n-1); $i++) {
        $out[$i] = array(array());

        // for every value in the input array
        for($j=$n-1; $j>=0; $j--) {
            $val = $in[$j];
            if(($i >> $j) & 1)
                $out[$i][] = array($val);
            else $out[$i][count($out[$i])-1][] = $val;
        }
    }

    return $out;
}

$subsets = getSpecialSubsets([1, 2, 3, 4]);

// for demonstration purposes only!!
echo "<pre>";
echo preg_replace('~\]\],~', "]],\n", json_encode($subsets));
echo "</pre>";
?>
于 2015-06-07T12:29:12.233 回答
0

这花了我几个小时来制作!!

你是对的,你的问题被错误地设置为重复。但是,在那个问题中,您想要的是获取集合的所有分区,而不是您此处想要的(这里您想要该集合的集。)在任何一种情况下,您都可以使用以下内容来获得所需的输出,只需将标记行中所需函数的名称替换为您想要的任何名称(我的意思是您也可以将其更改为上面getSpecialSubsets()显示的.

正如您所说的那样,所需的主要功能getVerySpecialSubsets()也具有$level_min$level_max:)的有利参数

<?php
function allPermutations($InArray, $InProcessedArray = array())
{
    $ReturnArray = array();
    foreach($InArray as $Key=>$value)
    {
        $CopyArray = $InProcessedArray;
        $CopyArray[$Key] = $value;
        $TempArray = array_diff_key($InArray, $CopyArray);

        if (count($TempArray) == 0)
            $ReturnArray[] = array_values($CopyArray);
        else
            $ReturnArray = array_merge($ReturnArray, allPermutations($TempArray, $CopyArray));
    }
    return $ReturnArray;
}

function powerSet($in,$minLength = 1) { 
   $count = count($in); 
   $members = pow(2,$count); 
   $return = array(); 
   for ($i = 0; $i < $members; $i++) { 
      $b = sprintf("%0".$count."b",$i); 
      $out = array(); 
      for ($j = 0; $j < $count; $j++) { 
         if ($b{$j} == '1') $out[] = $in[$j]; 
      } 
      if (count($out) >= $minLength) { 
         $return[] = $out; 
      } 
   } 
   return $return; 
}

function getAllPartitionsOfSet($in) {
    $arr = powerSet(powerSet($in));

    $out = array();
    foreach($arr as $combination) {
        $a = array();
        foreach($combination as $_arr)
            foreach($_arr as $v)
                $a[] = $v;

        // check if $a has duplicates
        // (i.e: the intersection of subsets in $combination is void)
        if(count($a) !== count(array_unique($a)))
            continue;

        // check if there's no difference between $a and $in.
        // (i.e: the union of subsets in $combination is equal to $a)
        if(!empty(array_diff($a, $in)) || !empty(array_diff($in, $a)))
            continue;

        $out[] = $combination;
    }
    return $out;
}

function getVerySpecialSubsets($_in, $level_min = 1, $level_max = 0) {
    $in = array_reverse($_in);
    $n = count($in);

    $level_min = $level_min>0 ? $level_min : 1;
    $level_max = $level_max>0 ? $level_max : $n;

    $allPartitions = getAllPartitionsOfSet($_in); //* marked!
    $out = array();

    foreach($allPartitions as $partition) {
        $levels_count = count($partition);
        if($levels_count>$level_max || $levels_count<$level_min)
            continue;

        // add the permutations of the arrays
        for($i=0; $i<count($partition); $i++) {
            $per = allPermutations($partition[$i]);
            if(count($per)==1)
                continue;

            // combine the permutation with the rest of array
            $first_item = true;
            foreach($per as $_per) {
                $arr = array();
                for($j=0; $j<count($partition); $j++)
                    $arr[] = ($j==$i) ? $_per : $partition[$j];
                $out[] = $arr;
            }
        }
    }

    // last singles
    if($n<=$level_max && $n>=$level_min) {
        $arr_last = array();
        for($k=count($in)-1; $k>=0; $k--)
            $arr_last[] = array($in[$k]);
        $out[] = $arr_last;
    }

    return array_values(array_unique($out, SORT_REGULAR));
}

$subsets = getVerySpecialSubsets([0, 1, 2]);

// for demonstration purposes only!!
echo '<pre>';
echo preg_replace('~\]\],~', "]],\n", json_encode($subsets));
echo '</pre>';
?>
于 2015-06-11T18:16:49.687 回答