
[LeetCode] 6. Zigzag Conversion

class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1 or numRows >= len(s):
return s
rows = [''] * numRows
i, step = 0, 1 # current row index and direction (+1 down, -1 up)
for ch in s:
rows[i] += ch
# flip direction at the top/bottom
if i == 0:
step = 1
elif i == numRows - 1:
step = -1
i += step
return ''.join(rows)