对字符串进行操作
有几行就先初始化几个tmp=’’
然后遍历字符串 找到每个字符应该存到第几行
顺序是从第一行到最后一行再到第一行到最后一行…如此反复
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1:
return s
temp = ['' for i in range(numRows)]
index= -1
step = 1
for i in range(len(s)):
index +=step
if index == numRows:
index -=2
step = -1
if index == -1:
index=1
step=1
temp[index] += s[i]
return ''.join(temp)

本文介绍了一种将字符串按Z形路径排列并重新读取的方法。通过遍历字符串,将其分配到多个行中,形成Z形路径。算法使用一个临时数组存储每一行的数据,再将这些数据连接成最终的Z形转换后的字符串。
3599

被折叠的 条评论
为什么被折叠?



