写一段代码,找到所有子集合,如[a,b,c]的子集合有{},{a},{b},{c},{ab},{ac},{abc}
//递归法
function allSubSet(array $arr = [], $sub_set = "", $begin_point)
{
$res = [];
if($sub_set) {
$res[] = "{" . trim($sub_set, ",") . "}";
}
for ($start_point = $begin_point; $start_point < count($arr); ++$start_point) {
$res = array_merge(
$res,
allSubSet($arr, $sub_set . "," . $arr[$start_point], $start_point + 1)
);
}
return $res;
}
$data = allSubSet([1, 2, 3], '', 0);
var_dump($data);
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »