From 0281b82d48a4f21429d35a89270843357f801c15 Mon Sep 17 00:00:00 2001 From: SevenMonths Date: Mon, 16 May 2022 15:26:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0php=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../剑指Offer58-II.左旋转字符串.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index fec83e1d..8781ffb4 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -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; + } +} +```