使用しているのは
文字列の逆順については、PHP関数strrev()のテストコードは以下の通りです:
<ol class="dp-xml"> <li class="alt"><span><span>header('Content-type: text/html; </span><span class="attribute">charset</span><span>=</span><span class="attribute-value">utf</span><span>-8'); </span></span></li> <li> <span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">implode</span><span>('', range(9, 0)); </span> </li> <li class="alt"> <span>print '</span><span class="tag"><</span><span> </span><span class="tag-name">p</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">strong</span><span class="tag">></span><span>Before reversed: </span><span class="tag"></</span><span class="tag-name">strong</span><span class="tag">></span><span>'.$str.'</span><span class="tag"><</span><span> /p</span><span class="tag">></span><span>'; </span> </li> <li> <span>print '</span><span class="tag"><</span><span> </span><span class="tag-name">p</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">strong</span><span class="tag">></span><span>After reversed: </span><span class="tag"><</span><span> /strong</span><span class="tag">></span><span>'.strrev($str).'</span><span class="tag"><</span><span> /p</span><span class="tag">></span><span>'; </span> </li> <li class="alt"><span>/* </span></li> <li><span>输出如下: </span></li> <li class="alt"><span>Before reversed: 9876543210 </span></li> <li><span>After reversed: 0123456789 </span></li> <li class="alt"><span>*/ </span></li> </ol>
組み込みのPHP関数strrev()が無い場合の実装方法使用済み?ここでは 3 つの方法 (二分法、ループ法、再帰的方法) がテストされましたが、パフォーマンス テストは行われませんでした。
1. 二分法
<ol class="dp-xml"> <li class="alt"><span><span>/** </span></span></li> <li><span>* 二分法实现字符串逆序排列 </span></li> <li class="alt"><span>* @param string $str 源字符串 </span></li> <li><span>* @return string 返回逆序后的字符串 </span></li> <li class="alt"><span>*/ </span></li> <li> <span>function reverse($</span><span class="attribute">str</span><span>=</span><span class="attribute-value">''</span><span>) { </span> </li> <li class="alt"> <span>$</span><span class="attribute">len</span><span> = </span><span class="attribute-value">strlen</span><span>($str);//不能使用count或sizeof </span> </li> <li> <span>$</span><span class="attribute">mid</span><span> = </span><span class="attribute-value">floor</span><span>($len/2); </span> </li> <li class="alt"> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>; $i</span><span class="tag"><</span><span>$mid; $i++) { </span> </li> <li> <span>$</span><span class="attribute">temp</span><span> = $str[$i]; </span> </li> <li class="alt"><span>$str[$i] = $str[$len-$i-1]; </span></li> <li><span>$str[$len-$i-1] = $temp; </span></li> <li class="alt"><span>} </span></li> <li><span>return $str; </span></li> <li class="alt"><span>} </span></li> </ol>
2. ループメソッド
3. 再帰メソッド
<ol class="dp-xml"> <li class="alt"><span><span>/** </span></span></li> <li><span>* 循环实现对字符串的逆序排列(效率比二分法低) </span></li> <li class="alt"><span>* @param string $str 源字符串 </span></li> <li><span>* @return string 返回逆序后的字符串 </span></li> <li class="alt"><span>*/ </span></li> <li> <span>function reverse($</span><span class="attribute">str</span><span>=</span><span class="attribute-value">''</span><span>) { </span> </li> <li class="alt"> <span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">''</span><span>; </span> </li> <li> <span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">1</span><span>; $i</span><span class="tag"><</span><span>=strlen($str); $i++) { </span> </li> <li class="alt"> <span>$result </span><span class="attribute">.</span><span>= </span><span class="attribute-value">substr</span><span>($str, -$i, 1); </span> </li> <li><span>} </span></li> <li class="alt"><span>return $result; </span></li> <li><span>} </span></li> </ol>