mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加 0541.反转字符串II go版
This commit is contained in:
@ -164,6 +164,30 @@ class Solution(object):
|
||||
|
||||
|
||||
Go:
|
||||
```go
|
||||
func reverseStr(s string, k int) string {
|
||||
ss := []byte(s)
|
||||
length := len(s)
|
||||
for i := 0; i < length; i += 2 * k {
|
||||
if i + k <= length {
|
||||
reverse(ss[i:i+k])
|
||||
} else {
|
||||
reverse(ss[i:length])
|
||||
}
|
||||
}
|
||||
return string(ss)
|
||||
}
|
||||
|
||||
func reverse(b []byte) {
|
||||
left := 0
|
||||
right := len(b) - 1
|
||||
for left < right {
|
||||
b[left], b[right] = b[right], b[left]
|
||||
left++
|
||||
right--
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
javaScript:
|
||||
|
||||
|
Reference in New Issue
Block a user