版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36372879/article/details/82390522
解题思路,按照列来存放,按照行来遍历就行。列的步长根据行变化,在数据结构上不需要设计成z字形的数组
坑1:二维数组创建错误:具体参考上一篇博客
坑2:numRows=1的情况下,步长设置错误
if numRows == 1:
return s
AC解答
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
rows = [(['!'] * 100) for i in range (numRows)]
rowindex = [0] * numRows #记录每一行元素的个数
row = 0
for i in s:
if row == 0:
step = 1
if row == numRows - 1:
step = -1
if numRows == 1:
return s
rows[row][rowindex[row]] = i
rowindex[row] += 1
row += step
#print(rows)
str1 = ''
for i in range(numRows):
for j in rows[i]:
if j != '!':
str1 = str1 + j
return str1