Accepted solution for problem #6 zigzag conversion.
This commit is contained in:
27
6-zigzag-conversion.go
Normal file
27
6-zigzag-conversion.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func ZigZagConvert(s string, numRows int) string {
|
||||||
|
res := make([]string, numRows)
|
||||||
|
slen := len(s)
|
||||||
|
resStr := ""
|
||||||
|
|
||||||
|
for i := 0; i < slen; {
|
||||||
|
j := 0
|
||||||
|
for ; j < numRows && i < slen; j++ {
|
||||||
|
res[j] += string(s[i])
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
k := j - 2
|
||||||
|
for k > 0 && i < slen {
|
||||||
|
res[k] += string(s[i])
|
||||||
|
i++
|
||||||
|
k--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < numRows; i++ {
|
||||||
|
resStr += res[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
return resStr
|
||||||
|
}
|
||||||
20
6-zigzag-conversion_test.go
Normal file
20
6-zigzag-conversion_test.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestZigZagConvert3(t *testing.T) {
|
||||||
|
input := "PAYPALISHIRING"
|
||||||
|
expected := "PAHNAPLSIIGYIR"
|
||||||
|
res := ZigZagConvert(input, 3)
|
||||||
|
assert.Equal(t, expected, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestZigZagConvert4(t *testing.T) {
|
||||||
|
input := "PAYPALISHIRING"
|
||||||
|
expected := "PINALSIGYAHRPI"
|
||||||
|
res := ZigZagConvert(input, 4)
|
||||||
|
assert.Equal(t, expected, res)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user