class Solution:def convert(self, s: str, numRows: int) -> str:#先判断z有多少隔开s_new=""index_now=0if len(s)<=numRows or numRows==1:return sfor i in range(numRows-1,-1,-1):exchange=0index_exchange=index_nows_new+=s[index_now]#计算每一层的差距gap_buttom=int(1+(i-1)*2)gap_top=int(1+(numRows-i-2)*2)if i==numRows-1 or i==0:while index_exchange+int(1+(numRows-2)*2)+1<=len(s)-1:s_new+=s[index_exchange+int(1+(numRows-2)*2)+1]index_exchange+=int(1+(numRows-2)*2)+1index_now+=1continuewhile i!=numRows-1 and i!=0:if exchange==0:if index_exchange+gap_buttom+1>len(s)-1:index_now+=1breakelse:s_new+=s[index_exchange+gap_buttom+1]index_exchange+=(gap_buttom+1)exchange=1else:if index_exchange+gap_top+1>len(s)-1:index_now+=1breakelse:s_new+=s[index_exchange+gap_top+1]index_exchange+=(gap_top+1)exchange=0return s_new