File tree Expand file tree Collapse file tree 2 files changed +79
-5
lines changed Expand file tree Collapse file tree 2 files changed +79
-5
lines changed Original file line number Diff line number Diff line change @@ -318,14 +318,66 @@ class Solution {
318
318
319
319
Python:
320
320
321
-
322
321
Go:
323
322
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
+
324
376
325
377
326
378
327
379
-----------------------
328
380
* 作者微信:[ 程序员Carl] ( https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw )
329
381
* B站视频:[ 代码随想录] ( https://space.bilibili.com/525438321 )
330
382
* 知识星球:[ 代码随想录] ( 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 >
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
23
23
示例 2:
24
24
输入: s = "lrloseumgh", k = 6
25
25
输出: "umghlrlose"
26
-
26
+
27
27
限制:
28
28
1 <= k < s.length <= 10000
29
29
@@ -119,14 +119,36 @@ class Solution {
119
119
```
120
120
Python:
121
121
122
-
123
122
Go:
124
123
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
+
125
147
126
148
127
149
128
150
-----------------------
129
151
* 作者微信:[ 程序员Carl] ( https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw )
130
152
* B站视频:[ 代码随想录] ( https://space.bilibili.com/525438321 )
131
153
* 知识星球:[ 代码随想录] ( 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 >
You can’t perform that action at this time.
0 commit comments