Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2197 from unrealjo/main
Browse files Browse the repository at this point in the history
Create 0006-zigzag-conversion.go
  • Loading branch information
tahsintunan authored Feb 2, 2023
2 parents 7881255 + b4b6cc0 commit 0959273
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions go/0006-zigzag-conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package zigzagconversion

import (
"strings"
)

func convert(s string, numRows int) string {
if numRows == 1 {
return s
}
var result strings.Builder
listLength := len(s)
step := (numRows - 1) * 2
for i := 0; i < numRows; i++ {
// reset steps at last row
for k := i; k < listLength; k += step {
result.WriteByte(s[k])
if i < numRows-1 && i > 0 && k+step-i*2 < listLength {
result.WriteByte(s[step+k-i*2])
}

}
}
return result.String()
}

0 comments on commit 0959273

Please sign in to comment.