原题
https://leetcode.cn/problems/zigzag-conversion/description
解法
遍历每行,按照对称规律都能找到下一个要遍历的下标是啥
func convert(s string, numRows int) string {
if numRows == 1{
return s
}
row := 1
interval := numRows * 2 - 2 // 例如 4, interval=6
res := ""
for row <= numRows{
halfInterval := (numRows - row) * 2 // 例如 4 - 1, halfInterval=3
max := row
for max <= len(s){
res += string(s[max-1])
if halfInterval != interval && halfInterval != 0{
half := max + halfInterval
if half <= len(s){
res += string(s[half-1])
}
}
max += interval
}
row++
}
return res
}
原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/leetcode6-z-%e5%ad%97%e5%bd%a2%e5%8f%98%e6%8d%a2/