PHP recursive 01 with recursive implementation in alphabetical order

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.