-
Notifications
You must be signed in to change notification settings - Fork 0
/
zigzag-conversion.go
76 lines (72 loc) · 1.59 KB
/
zigzag-conversion.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"strings"
)
func convert(s string, numRows int) string {
if len(s) <= numRows || numRows <= 1 {
return s
}
matrix := [][]byte{}
var inner []byte
for i := 0; i < len(s); {
if len(inner) <= 0 {
inner = make([]byte, 0)
}
isZig := numRows > 2 && len(matrix) > 0 && len(matrix)%2 != 0
if len(inner) == numRows || isZig && len(inner) == numRows-2 {
matrix = append(matrix, inner)
inner = nil
continue
}
inner = append(inner, s[i])
if i == len(s)-1 {
if isZig {
zeroToAdd := numRows - 2 - len(inner)
for j := 0; j < zeroToAdd; j++ {
inner = append(inner, byte(0))
}
}
matrix = append(matrix, inner)
}
i++
}
for i, v := range matrix {
if isZig := i%2 != 0 && numRows > 2; isZig {
if len(v) > 1 {
reverse(v)
}
temp := []byte{}
temp = append(temp, 0)
temp = append(temp, v...)
temp = append(temp, 0)
v = temp
}
if len(v) < numRows {
zeroToAdd := numRows - len(v)
for j := 0; j < zeroToAdd; j++ {
v = append(v, 0)
}
}
matrix[i] = v
}
result := make([]string, len(s))
for i := 0; i < numRows; i++ {
for j := 0; j < len(matrix); j++ {
if item := matrix[j][i]; item != 0 {
result = append(result, string(item))
}
}
}
return strings.Join(result, "")
}
func reverse(slice []byte) {
for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
slice[i], slice[j] = slice[j], slice[i]
}
}
func main() {
// fmt.Println(convert("ABC", 2))
// fmt.Println(convert("ABCD", 2))
fmt.Println(convert("ABCDE", 4))
}