算法设计:
初始化一个n维数组L,以及step为1;当n为0时,step为1,当n为row-1时,step变为-1;这样既能保证n在[0, row-1]之间按顺序变动。
源代码:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
#edge case
if numRows <= 1 or numRows >= len(s):
return s
#init L and step
L = [''] * numRows
index, step = 0, 1
for x in s:
L[index] += x
if index == 0:
step = 1
elif index == numRows - 1:
step = -1
index += step
return ''.join(L)