mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 08:02:30 +08:00
27 lines
550 B
Go
27 lines
550 B
Go
package leetcode
|
|
|
|
func convert(s string, numRows int) string {
|
|
matrix, down, up := make([][]byte, numRows, numRows), 0, numRows-2
|
|
for i := 0; i != len(s); {
|
|
if down != numRows {
|
|
matrix[down] = append(matrix[down], byte(s[i]))
|
|
down++
|
|
i++
|
|
} else if up > 0 {
|
|
matrix[up] = append(matrix[up], byte(s[i]))
|
|
up--
|
|
i++
|
|
} else {
|
|
up = numRows - 2
|
|
down = 0
|
|
}
|
|
}
|
|
solution := make([]byte, 0, len(s))
|
|
for _, row := range matrix {
|
|
for _, item := range row {
|
|
solution = append(solution, item)
|
|
}
|
|
}
|
|
return string(solution)
|
|
}
|