Skip to content

Commit f241b0a

Browse files
Merge pull request #311 from liumingzhuo/master
添加0151.翻转字符串和剑指Offer58-II.左旋转字符串Go版本
2 parents 405463d + fae5373 commit f241b0a

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

problems/0151.翻转字符串里的单词.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,66 @@ class Solution {
318318

319319
Python:
320320

321-
322321
Go:
323322

323+
```go
324+
import (
325+
"fmt"
326+
)
327+
328+
func reverseWords(s string) string {
329+
//1.使用双指针删除冗余的空格
330+
slowIndex, fastIndex := 0, 0
331+
b := []byte(s)
332+
//删除头部冗余空格
333+
for len(b) > 0 && fastIndex < len(b) && b[fastIndex] == ' ' {
334+
fastIndex++
335+
}
336+
//删除单词间冗余空格
337+
for ; fastIndex < len(b); fastIndex++ {
338+
if fastIndex-1 > 0 && b[fastIndex-1] == b[fastIndex] && b[fastIndex] == ' ' {
339+
continue
340+
}
341+
b[slowIndex] = b[fastIndex]
342+
slowIndex++
343+
}
344+
//删除尾部冗余空格
345+
if slowIndex-1 > 0 && b[slowIndex-1] == ' ' {
346+
b = b[:slowIndex-1]
347+
} else {
348+
b = b[:slowIndex]
349+
}
350+
//2.反转整个字符串
351+
reverse(&b, 0, len(b)-1)
352+
//3.反转单个单词 i单词开始位置,j单词结束位置
353+
i := 0
354+
for i < len(b) {
355+
j := i
356+
for ; j < len(b) && b[j] != ' '; j++ {
357+
}
358+
reverse(&b, i, j-1)
359+
i = j
360+
i++
361+
}
362+
return string(b)
363+
}
364+
365+
func reverse(b *[]byte, left, right int) {
366+
for left < right {
367+
(*b)[left], (*b)[right] = (*b)[right], (*b)[left]
368+
left++
369+
right--
370+
}
371+
}
372+
```
373+
374+
375+
324376

325377

326378

327379
-----------------------
328380
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
329381
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
330382
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
331-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
383+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/剑指Offer58-II.左旋转字符串.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
2323
示例 2:
2424
输入: s = "lrloseumgh", k = 6
2525
输出: "umghlrlose"
26-
 
26+
2727
限制:
2828
1 <= k < s.length <= 10000
2929

@@ -119,14 +119,36 @@ class Solution {
119119
```
120120
Python:
121121

122-
123122
Go:
124123

124+
```go
125+
func reverseLeftWords(s string, n int) string {
126+
b := []byte(s)
127+
// 1. 反转前n个字符
128+
// 2. 反转第n到end字符
129+
// 3. 反转整个字符
130+
reverse(b, 0, n-1)
131+
reverse(b, n, len(b)-1)
132+
reverse(b, 0, len(b)-1)
133+
return string(b)
134+
}
135+
// 切片是引用传递
136+
func reverse(b []byte, left, right int){
137+
for left < right{
138+
b[left], b[right] = b[right],b[left]
139+
left++
140+
right--
141+
}
142+
}
143+
```
144+
145+
146+
125147

126148

127149

128150
-----------------------
129151
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
130152
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
131153
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
132-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
154+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

0 commit comments

Comments
 (0)