-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (43 loc) · 900 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import "fmt"
// 双指针法
func replaceSpace(s string) string {
b := []byte(s)
bLen := len(b)
// 统计空格数
spaceCount := 0
for _, v := range b {
if v == byte(' ') {
spaceCount += 1
}
}
// 扩容数组
scalaArr := make([]byte, spaceCount*2) // 这里记得需要乘2
b = append(b, scalaArr...)
// 遍历数组进行替换和移动
// 双指针法, 一个指针指向新数组的末尾,一个指针指向旧指针的末尾
left := bLen - 1
right := len(b) - 1
for left >= 0 {
// 遇到空格
if b[left] == ' ' {
// 填充%20
b[right] = '0'
b[right-1] = '2'
b[right-2] = '%'
left -= 1
right -= 3
} else {
// 直接替换left索引对应的数组值
b[right] = b[left]
left -= 1
right -= 1
}
}
return string(b)
}
func main() {
println("UseCase 1......")
s := "We are happy."
fmt.Printf("%s\n", replaceSpace(s))
}