Skip to content

Commit 29dc9df

Browse files
Merge pull request #319 from betNevS/master
添加 0541.反转字符串II go版
2 parents a00f188 + 17dd8f8 commit 29dc9df

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

problems/0541.反转字符串II.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,30 @@ class Solution(object):
164164

165165

166166
Go:
167+
```go
168+
func reverseStr(s string, k int) string {
169+
ss := []byte(s)
170+
length := len(s)
171+
for i := 0; i < length; i += 2 * k {
172+
if i + k <= length {
173+
reverse(ss[i:i+k])
174+
} else {
175+
reverse(ss[i:length])
176+
}
177+
}
178+
return string(ss)
179+
}
180+
181+
func reverse(b []byte) {
182+
left := 0
183+
right := len(b) - 1
184+
for left < right {
185+
b[left], b[right] = b[right], b[left]
186+
left++
187+
right--
188+
}
189+
}
190+
```
167191

168192
javaScript:
169193

0 commit comments

Comments
 (0)