-
-
Save zubinJiang/3916743 to your computer and use it in GitHub Desktop.
php 全排列递归算法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fsRank($base, $temp=null) | |
{ | |
static $ret = array(); | |
$len = strlen($base); | |
if($len <= 1) | |
{ | |
//echo $temp.$base.'<br/>'; | |
$ret[] = $temp.$base; | |
} | |
else | |
{ | |
for($i=0; $i< $len; ++$i) | |
{ | |
$had_flag = false; | |
for($j=0; $j<$i; ++$j) | |
{ | |
if($base[$i] == $base[$j]) | |
{ | |
$had_flag = true; | |
break; | |
} | |
} | |
if($had_flag) | |
{ | |
continue; | |
} | |
fsRank(substr($base, 0, $i).substr($base, $i+1, $len-$i-1), $temp.$base[$i]); | |
} | |
} | |
return $ret; | |
} | |
print '<pre>'; | |
print_r(fsRank('122')); | |
print '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment