增加php版本

This commit is contained in:
SevenMonths
2022-05-16 15:26:21 +08:00
parent 98458cdc56
commit 0281b82d48

View File

@ -290,6 +290,25 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) {
} }
``` ```
### PHP
```php
function reverseLeftWords($s, $n) {
$this->reverse($s,0,$n-1); //反转区间为前n的子串
$this->reverse($s,$n,strlen($s)-1); //反转区间为n到末尾的子串
$this->reverse($s,0,strlen($s)-1); //反转整个字符串
return $s;
}
// 按指定进行翻转 【array、string都可】
function reverse(&$s, $start, $end) {
for ($i = $start, $j = $end; $i < $j; $i++, $j--) {
$tmp = $s[$i];
$s[$i] = $s[$j];
$s[$j] = $tmp;
}
}
```