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

Function convert

tasks/6.go:9–43  ·  view source on GitHub ↗
(s string, numRows int)

Source from the content-addressed store, hash-verified

7}
8
9func convert(s string, numRows int) string {
10
11 if len(s) <= numRows || numRows == 1 {
12 return s
13 }
14 // min(diag) == 2
15 // diag == numRows - 2
16 maxStep := numRows + numRows - 2
17 ret := make([]byte, len(s))
18
19 currInd := 0
20 currRow := 0
21 for currRow < numRows {
22
23 // first and last are symmetrical
24 if currRow == 0 || currRow == numRows-1 {
25 for i := currRow; i < len(s); i += maxStep {
26 ret[currInd] = s[i]
27 currInd++
28 }
29 } else {
30 for i := currRow; i < len(s); i += maxStep {
31 ret[currInd] = s[i]
32 currInd++
33 pairedElInd := i + maxStep - 2*currRow
34 if pairedElInd < len(s) {
35 ret[currInd] = s[pairedElInd]
36 currInd++
37 }
38 }
39 }
40 currRow++
41 }
42 return string(ret)
43}

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected