Merge pull request #1339 from fmtvar/offer58-2

添加(剑指Offer58-II.左旋转字符串.md):增加PHP版本
This commit is contained in:
程序员Carl
2022-06-06 11:38:41 +08:00
committed by GitHub

View File

@ -290,6 +290,28 @@ 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;
}
}
```
Scala:
```scala
@ -322,5 +344,6 @@ object Solution {
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>