字符串处理 - php实现字符串反转[首尾交换]
大家讲道理
大家讲道理 2017-04-10 15:21:18
0
2
516

php实现字符串反转,不用strrev,不借用数组方式,时间复杂度度小于O(n)的,首尾交换的那种实现。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

répondre à tous(2)
小葫芦
<?php
$str = 'hello world';
$tmp = '';
for($i = strlen($str)-1; $i >= 0; $i--){
    $tmp .= $str{$};
}

echo $tmp;
Ty80

貌似不存在O(n/2)这种说法,也还是O(n)

php<?php

$str = 'I am Mr.Jing';

// 我去!php中字符串的元素居然是可变的
for ($i=0, $j = strlen($str)-1; $i < $j; $i++, $j--) {
    $tmp = $str[$j];
    $str[$j] = $str[$i];
    $str[$i] = $tmp;
}
// 输出结果
echo $str;
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!