Recently interested in recursive comparisons, so began to record some of the learning process of recursive use method
1. The full permutation function (arrange) has two parameters one is a string that needs to be fully arranged (assuming the default is in dictionary order) and the other is the accumulated prefix such as:
The first time by default is '
The second time, after the For loop, represents 1, 2, 3, and is the prefix parameter for the next sub-recursive
The third time, respectively, is 12 13 21 23 31 32 The remaining string length that needs to be fully arranged is one, using echo to output with return end recursion
The 2.deal function also has two parameters, namely, the currently fully arranged string, and the removal of substrings appended to the parent prefix that need to be fully arranged
1 //the full array of ordered numeric strings, such as 1234562 //1233 //arrange[1,2,3] = 1.arrange[2,3] + 2.arrange[1,3] + 3.arrange[1,2]4 //1.arrange[2,3] = 12.arrange[3] + 13.arrange[2]5 functionArrange$str,$prefix= ' ') {6 /*7 * if (!is_array ($STR)) {8 * $str = Str_split ($STR);9 * }Ten */ One $str=Str_split($str); A $len=Count($str); - if($len= = 1) { - Echo $prefix.$str[0]. " \ n "; the return; - } - for($i= 0;$i<$len;$i++) { - $tmp=$prefix.$str[$i]; +Arrange (Deal ($str,$i),$tmp); - } + } A functionDeal$str,$index) { at unset($str[$index]); - return implode(‘‘,$str); - } -Arrange (' 123 ')
PHP recursive 01 with recursive implementation in alphabetical order