123456789101112131415161718192021222324252627282930 |
- func compress(chars []byte) int {
- fast, slow, cnt, n := 1, 0, 1, len(chars)
- if n <= 1 {
- return n
- }
- for ; fast < n; fast++ {
- if chars[fast] != chars[fast-1] {
- chars[slow] = chars[fast-1]
- slow++
- if cnt != 1 {
- num := strconv.Itoa(cnt)
- for end, i := slow+len(num), 0; slow < end; slow, i = slow+1, i+1 {
- chars[slow] = num[i]
- }
- cnt = 1
- }
- } else {
- cnt++
- }
- }
- chars[slow] = chars[fast-1]
- slow++
- if cnt != 1 {
- num := strconv.Itoa(cnt)
- for end, i := slow+len(num), 0; slow < end; slow, i = slow+1, i+1 {
- chars[slow] = num[i]
- }
- }
- return slow
- }
|