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

例如,输入字符串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实现字符串的全排列详解

标签:
PHP,字符串,排列

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“PHP实现字符串的全排列详解”

暂无“PHP实现字符串的全排列详解”评论...