Skip to content

Commit 3cba050

Browse files
committed
添加0055.右旋字符串.md PHP版本
1 parent f96b0cf commit 3cba050

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

problems/kamacoder/0055.右旋字符串.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
```
2424
2
2525
abcdefg
26-
```
26+
```
2727

2828
样例输出:
2929

@@ -336,6 +336,32 @@ var reverseLeftWords = function(s, n) {
336336

337337
### PHP:
338338

339+
```php
340+
<?php
341+
// 反转函数
342+
function reverse(&$s, $start, $end) {
343+
for ($i = $start, $j = $end; $i < $j; $i++, $j--) {
344+
$tmp = $s[$i];
345+
$s[$i] = $s[$j];
346+
$s[$j] = $tmp;
347+
}
348+
}
349+
350+
// 标准输入:读取右旋转位数和字符串
351+
$n = trim(fgets(STDIN));
352+
$s = trim(fgets(STDIN));
353+
// 字符串长度
354+
$len = strlen($s);
355+
// 先部分反转
356+
reverse($s, $len - $n, $len - 1);
357+
reverse($s, 0, $len - $n - 1);
358+
// 再整体反转
359+
reverse($s, 0, $len - 1);
360+
361+
echo $s;
362+
?>
363+
```
364+
339365

340366
### Scala:
341367

@@ -349,3 +375,4 @@ var reverseLeftWords = function(s, n) {
349375
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
350376
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
351377
</a>
378+

0 commit comments

Comments
 (0)