添加剑指Offer58-II.左旋转字符串Go版本

This commit is contained in:
Neil.Liu
2021-06-02 10:25:09 +08:00
parent bb5cfa68b2
commit fae5373477

View File

@ -23,7 +23,7 @@ https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
示例 2 示例 2
输入: s = "lrloseumgh", k = 6 输入: s = "lrloseumgh", k = 6
输出: "umghlrlose" 输出: "umghlrlose"
 
限制: 限制:
1 <= k < s.length <= 10000 1 <= k < s.length <= 10000
@ -119,9 +119,31 @@ class Solution {
``` ```
Python Python
Go Go
```go
func reverseLeftWords(s string, n int) string {
b := []byte(s)
// 1. 反转前n个字符
// 2. 反转第n到end字符
// 3. 反转整个字符
reverse(b, 0, n-1)
reverse(b, n, len(b)-1)
reverse(b, 0, len(b)-1)
return string(b)
}
// 切片是引用传递
func reverse(b []byte, left, right int){
for left < right{
b[left], b[right] = b[right],b[left]
left++
right--
}
}
```
@ -129,4 +151,4 @@ Go
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321) * B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div> <div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>