DDR爱好者之家 Design By 杰米

输入一个字符串,按字典序打印出该字符串中字符的所有排列。

例如,输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
思路:
1.利用递归形成递归树,达到深度优先,固定首字母的效果

2.得复位以后才能再次深度优先

3.回溯法思想

4.一张图和一个运行过程,只能慢慢体会了

<"abc";$res=array();//调用入口,从索引0开始

$res=test($str,0,$res);

var_dump($res);
string(7) "abc===0"array(0) {

}string(7) "abc===1"array(0) {

}string(7) "abc===2"array(0) {

}string(7) "acb===1"array(1) {

 [0]=> string(3) "abc"}string(7) "acb===2"array(1) {

 [0]=> string(3) "abc"}string(7) "bac===0"array(2) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"}string(7) "bac===1"array(2) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"}string(7) "bac===2"array(2) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"}string(7) "bca===1"array(3) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"

 [2]=> string(3) "bac"}string(7) "bca===2"array(3) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"

 [2]=> string(3) "bac"}string(7) "cba===0"array(4) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"

 [2]=> string(3) "bac"

 [3]=> string(3) "bca"}string(7) "cba===1"array(4) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"

 [2]=> string(3) "bac"

 [3]=> string(3) "bca"}string(7) "cba===2"array(4) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"

 [2]=> string(3) "bac"

 [3]=> string(3) "bca"}string(7) "cab===1"array(5) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"

 [2]=> string(3) "bac"

 [3]=> string(3) "bca"

 [4]=> string(3) "cba"}string(7) "cab===2"array(5) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"

 [2]=> string(3) "bac"

 [3]=> string(3) "bca"

 [4]=> string(3) "cba"}array(6) {

 [0]=> string(3) "abc"

 [1]=> string(3) "acb"

 [2]=> string(3) "bac"

 [3]=> string(3) "bca"

 [4]=> string(3) "cba"

 [5]=> string(3) "cab"}

PHP实现字符串的全排列详解

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米