第一次 196ms 18%:
class Solution:
def convert(self, s, numRows):
l = []
r = ''
for i in range(numRows):
l.append([])
pos = -1
forword = True
for i in range(len(s)):
if forword:
pos += 1
if pos >= numRows - 1:
forword = False
else:
pos -= 1
if pos <= 0:
forword = True
l[pos].append(s[i])
for i in range(numRows):
for j in range(len(l[i])):
r += l[i][j]
return r
第二次 104ms 95%:
class Solution:
def convert(self, s, numRows):
l = []
for i in range(numRows):
l.append('')
pos = -1
forword = True
for i in range(len(s)):
if forword:
pos += 1
if pos >= numRows - 1:
forword = False
else:
pos -= 1
if pos <= 0:
forword = True
l[pos] += s[i]
return ''.join(l)