题目:6. ZigZag Conversion
链接:https://leetcode.com/problems/zigzag-conversion/description/
嗯怎么说呢,大概是将字符串像拉链一样穿起来吧。
Python:
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if len(s)<=numRows:
return s
res=[""]*numRows
length=len(s)
i=0
while i<length:
cnt=0
while cnt<numRows and i<length:
res[cnt]+=s[i]
cnt+=1
i+=1
cnt=numRows-2
while cnt>0 and i<length:
res[cnt]+=s[i]
cnt-=1
i+=1
ans=""
for i in range(numRows):
ans+=res[i]
return ans