-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path443.go
More file actions
41 lines (37 loc) · 757 Bytes
/
Copy path443.go
File metadata and controls
41 lines (37 loc) · 757 Bytes
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
package main
import "strconv"
import "fmt"
func compress(chars []byte) int {
l := 0
var g byte
glen := 0
g = chars[0]
for _, c := range(chars) {
if c == g {
glen++
} else {
if glen > 1 {
chars[l] = g
l++
l += copy(chars[l:], strconv.Itoa(glen))
} else {
chars[l] = g
l++
}
g = c
glen = 1
}
}
chars[l] = g
l++
if glen > 1 {
l += copy(chars[l:], strconv.Itoa(glen))
}
return l
}
func main() {
chars := []byte{'a','b','b','c','c','c'}
l := compress(chars)
fmt.Println("l=", l)
fmt.Println(string(chars[0:l]))
}