这个想法很简单,用一个列表表示每一行的字符串,最后再输出即可。
重点是找规律。
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows==1: return s
res=["" for i in range(len(s))]
i,l=0,-1
while i<len(s):
while l<numRows-1 and i<len(s):
l+=1
res[l]+=s[i]
i+=1
while l>0 and i<len(s):
l-=1
res[l]+=s[i]
i+=1
return ''.join(res)