mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
添加0055.右旋字符串.md PHP版本
This commit is contained in:
@ -336,6 +336,32 @@ var reverseLeftWords = function(s, n) {
|
||||
|
||||
### PHP:
|
||||
|
||||
```php
|
||||
<?php
|
||||
// 反转函数
|
||||
function reverse(&$s, $start, $end) {
|
||||
for ($i = $start, $j = $end; $i < $j; $i++, $j--) {
|
||||
$tmp = $s[$i];
|
||||
$s[$i] = $s[$j];
|
||||
$s[$j] = $tmp;
|
||||
}
|
||||
}
|
||||
|
||||
// 标准输入:读取右旋转位数和字符串
|
||||
$n = trim(fgets(STDIN));
|
||||
$s = trim(fgets(STDIN));
|
||||
// 字符串长度
|
||||
$len = strlen($s);
|
||||
// 先部分反转
|
||||
reverse($s, $len - $n, $len - 1);
|
||||
reverse($s, 0, $len - $n - 1);
|
||||
// 再整体反转
|
||||
reverse($s, 0, $len - 1);
|
||||
|
||||
echo $s;
|
||||
?>
|
||||
```
|
||||
|
||||
|
||||
### Scala:
|
||||
|
||||
@ -349,3 +375,4 @@ var reverseLeftWords = function(s, n) {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user