PHP实现数组全排列算法(附完整源码)
发布时间
阅读量:
阅读量
PHP实现数组全排列算法
以下展示了一段用于实现数组全排列算法的完整 PHP 源代码:
<?php
/** * 数组全排列算法
* @param array $arr 输入数组
* @return array 排列结果
*/
function permute($arr) {
$result = [];
if (count($arr) <= 1) {
return [$arr];
}
foreach ($arr as $key => $value) {
$temp = $arr;
unset($temp[$key]);
$temp = array_values($temp);
$permutations = permute($temp);
foreach ($permutations as $permutation) {
$result[] = array_merge([$value], $permutation);
}
}
return $result;
}
// 测试用例
$
全部评论 (0)
还没有任何评论哟~
