Skip to content

Commit b4b6cc0

Browse files
author
Youssef Eddaif
committed
Create 0006-zigzag-conversion.go
1 parent 17e27eb commit b4b6cc0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: go/0006-zigzag-conversion.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package zigzagconversion
2+
3+
import (
4+
"strings"
5+
)
6+
7+
func convert(s string, numRows int) string {
8+
if numRows == 1 {
9+
return s
10+
}
11+
var result strings.Builder
12+
listLength := len(s)
13+
step := (numRows - 1) * 2
14+
for i := 0; i < numRows; i++ {
15+
// reset steps at last row
16+
for k := i; k < listLength; k += step {
17+
result.WriteByte(s[k])
18+
if i < numRows-1 && i > 0 && k+step-i*2 < listLength {
19+
result.WriteByte(s[step+k-i*2])
20+
}
21+
22+
}
23+
}
24+
return result.String()
25+
}

0 commit comments

Comments
 (0)