An easy understanding Python solution


  • 0
    C

    class Solution:

    # @param {string} s
    # @param {integer} numRows
    # @return {string}
    def convert(self, s, numRows):
        if numRows >= len(s)  or numRows == 1:
            return s
        
        delta = 2 * numRows - 2
        result = []
        for row in xrange(numRows):
            for pos in xrange(row, len(s), delta):
                if row == 0 or row == numRows - 1:
                    result.append(s[pos])
                else:
                    result.append(s[pos])
                    if (pos + delta - 2 * row) < len(s):
                        result.append(s[pos + delta - 2 * row]) 
        return ''.join(result)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.