1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution(object): def convert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str """ if numRows == 1 or numRows >= len(s): return s zigzag = [[] for x in range(numRows)] row, step = 0, 1 for c in s: zigzag[row] += c, if row == 0: step = 1 elif row == numRows - 1: step = -1 row += step # return ''.join(reduce(operator.add, zigzag)) # [[u'P', u'A', u'H', u'N'], [u'A', u'P', u'L', u'S', u'I', u'I', u'G'], [u'Y', u'I', u'R']] result = '' for i in range(len(zigzag)): result += ''.join(zigzag[i]) # list拼接为字符串 return result
|