一道找规律的题,找出各个字母位置之间的规律即可。不过要注意当numRow为1时需要特殊处理
1 class Solution: 2 def convert(self, s: str, numRows: int) -> str: 3 length=len(s) 4 if(numRows==1): 5 return s 6 num=numRows*2-2 7 group=length//num+1 8 9 def gets(i:int): 10 if i>=length: 11 return "" 12 else: 13 return s[i] 14 15 ans='' 16 for i in range(numRows): 17 for j in range(group): 18 head=j*num 19 if i==0: 20 ans+=gets(head+i) 21 elif i==num//2: 22 ans+=gets(head+i) 23 else: 24 ans+=gets(head+i) 25 ans+=gets(head+num-i) 26 return ans 27