MCPcopy Create free account
hub / github.com/VanjaRo/LeetCode / compress

Function compress

tasks/443.go:5–36  ·  view source on GitHub ↗
(chars []byte)

Source from the content-addressed store, hash-verified

3import "fmt"
4
5func compress(chars []byte) int {
6 if len(chars) == 0 {
7 return 0
8 }
9 start := 0
10 end := 0
11 pivot := 0
12 for i := 1; i < len(chars); i++ {
13 if chars[i] != chars[i-1] {
14 chars[pivot] = chars[start]
15 pivot++
16 if start != end {
17 for _, x := range fmt.Sprint(end - start + 1) {
18 chars[pivot] = byte(x)
19 pivot++
20 }
21 }
22 start = i
23 }
24 end = i
25 }
26 chars[pivot] = chars[start]
27 pivot++
28 if start != end {
29 for _, x := range fmt.Sprint(end - start + 1) {
30 chars[pivot] = byte(x)
31 pivot++
32 }
33 }
34 start = i
35 return pivot
36}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected