增加(剑指Offer05.替换空格.md):php版本

This commit is contained in:
SevenMonths
2022-05-24 16:53:39 +08:00
parent b78e750f8f
commit 3665c051d7

View File

@ -415,6 +415,40 @@ func replaceSpace(_ s: String) -> String {
PHP
```php
function replaceSpace($s){
$sLen = strlen($s);
$moreLen = $this->spaceLen($s) * 2;
$head = $sLen - 1;
$tail = $sLen + $moreLen - 1;
$s = $s . str_repeat(' ', $moreLen);
while ($head != $tail) {
if ($s[$head] == ' ') {
$s[$tail--] = '0';
$s[$tail--] = '2';
$s[$tail] = '%';
} else {
$s[$tail] = $s[$head];
}
$head--;
$tail--;
}
return $s;
}
// 统计空格个数
function spaceLen($s){
$count = 0;
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] == ' ') {
$count++;
}
}
return $count;
}
```
-----------------------