PHP字符串全排列

WBOY
Libérer: 2016-07-29 09:02:49
original
1760 Les gens l'ont consulté

方法一:

<code><span>$str</span> = <span>'abc'</span>;
<span>$a</span> =str_split(<span>$str</span>);
perm(<span>$a</span>, <span>0</span>, count(<span>$a</span>)-<span>1</span>);

<span><span>function</span><span>perm</span><span>(&<span>$ar</span>, <span>$k</span>, <span>$m</span>)</span> {</span><span>if</span>(<span>$k</span> == <span>$m</span>){ 
        <span>echo</span> join(<span>''</span>,<span>$ar</span>), PHP_EOL;
    }<span>else</span> {
        <span>for</span>(<span>$i</span>=<span>$k</span>; <span>$i</span>$m; <span>$i</span>++) {
            swap(<span>$ar</span>[<span>$k</span>], <span>$ar</span>[<span>$i</span>]);
            perm(<span>$ar</span>, <span>$k</span>+<span>1</span>, <span>$m</span>);
            swap(<span>$ar</span>[<span>$k</span>], <span>$ar</span>[<span>$i</span>]);
        }
    }
}
<span><span>function</span><span>swap</span><span>(&<span>$a</span>, &<span>$b</span>)</span> {</span><span>$c</span> = <span>$a</span>;
    <span>$a</span> = <span>$b</span>;
    <span>$b</span> = <span>$c</span>;
}</code>
Copier après la connexion

方法二:

<code><span><span>function</span><span>output</span><span>(<span>$temp</span>,<span>$level</span>)</span> {</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$level;<span>$i</span>++) {
        <span>echo</span><span>$temp</span>[<span>$i</span>];
    }
    <span>echo</span> PHP_EOL;
}
<span>//产生全排列 递归参数传递一定要注意 </span><span><span>function</span><span>quanpai</span><span>(<span>$arr</span>,<span>$flag</span>,<span>$level</span>,<span>$num</span>,<span>$temp</span>)</span> {</span><span>if</span>(<span>$level</span>>=<span>$num</span>) {
        output(<span>$temp</span>,<span>$num</span>);
        <span>return</span>;
    }
    <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$num;<span>$i</span>++) {
        <span>if</span>(<span>$flag</span>[<span>$i</span>]==<span>0</span>) {
            <span>$temp</span>[<span>$level</span>]=<span>$arr</span>[<span>$i</span>];
            <span>$flag</span>[<span>$i</span>]=<span>1</span>;
            quanpai(<span>$arr</span>,<span>$flag</span>,<span>$level</span>+<span>1</span>,<span>$num</span>,<span>$temp</span>);
            <span>$flag</span>[<span>$i</span>]=<span>0</span>;
        }
    }
}
<span>$str</span> = <span>'abc'</span>;
<span>$arr</span>=str_split(<span>$str</span>);
<span>$len</span>=count(<span>$arr</span>);
<span>$i</span>=<span>0</span>;
<span>$flag</span>=<span>array</span>();
<span>$temp</span>=<span>array</span>();
<span>while</span>(<span>$i</span>$len){
    <span>$flag</span>[<span>$i</span>]=<span>0</span>;
    <span>$i</span>++;
}
quanpai(<span>$arr</span>,<span>$flag</span>,<span>0</span>,<span>$len</span>,<span>$temp</span>);</code>
Copier après la connexion
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了PHP字符串全排列,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal