classSolution:defconvert(self, s, numRows):"""
:type s: str
:type numRows: int
:rtype: str
"""defhelp(s, numRows):#将字符串s转换成list[[list]]
lens = len(s)
L = []
count = numRows - 1
nums = numRows*2-2
i = 0while i<lens:
for j in range(count):
temp = []
print(i,j)
if j==0:
for p in range(numRows):
temp.append(s[i])
i += 1if i>= lens:
L.append(temp)
return L
else:
print('......',i)
for p in range(numRows):
if p == numRows - j - 1:
print(",,,",i)
temp.append(s[i])
i += 1if i>=lens:
L.append(temp)
return L
else:
temp.append(1)
print(i)
L.append(temp)
if len(s) <= numRows:
return s
if numRows == 1:
return s
returnL = ""
L = help(s, numRows)
len1 = len(L[-1])
for j in range(numRows):
for i in range(len(L)-1):
if L[i][j] != 1:
returnL += L[i][j]
if j<len1:
if L[-1][j] != 1:
returnL += L[-1][j]
return returnL
学习他人代码
classSolution:defconvert(self, s, numRows):"""
:type s: str
:type numRows: int
:rtype: str
"""if numRows == 1: return s
ans = ""
interval = 2 * (numRows - 1)
for i in range(0, len(s), interval):
ans += s[i]
for row in range(1, numRows - 1):
inter = 2 * row
i = row
while i < len(s):
ans += s[i]
inter = interval - inter
i += inter
print(ans)
for i in range(numRows - 1, len(s), interval):
ans += s[i]
return ans