说明一下,使用 global 可能看上去不那么优雅,但是这里我就是写一个示例方法,不就过于纠结啦,可自行优化
$str = '12,34,5';
$arr = explode(',', $str);
$step = $book = $result = [];
dfs(0);
print_r($result);
$str = '12,34';
$arr = explode(',', $str);
$step = $book = $result = [];
dfs(0);
print_r($result);
$str = '12,34,5,67';
$arr = explode(',', $str);
$step = $book = $result = [];
dfs(0);
print_r($result);
function dfs($s)
{
global $arr, $step, $result, $book;
if (!isset($arr[$s])) {
$result[] = array_values($step);
return;
}
for ($i = 0; $i < strlen($arr[$s]); $i++) {
if (!isset($book[$s][$i]) || $book[$s][$i] == 0) {
$book[$s][$i] = 1;
$step[$s] = $arr[$s][$i];
dfs($s + 1);
$book[$s][$i] = 0;
}
}
return;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…