原题:
一开始居然没看懂,解释一下:
按照Z字形状来重新遍历字符串
思路:
创建一个二维数组,然后按照一定的规律变化下标,把字符串的数字填入到二维数组中去。
代码:
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1:
return s
zigzag = [''] * numRows
row,step = 0,1
for c in s:
if row == 0:
step = 1
if row == numRows - 1:
step = -1
zigzag[row] += c
row += step
return ''.join(zigzag)
结果:
AC 88ms