My python solution O(n) running time


  • 1
    X

    The problem can be treated as converting len(s) into (2nRows - 2)sequence.

    class Solution:
        # @return a string
        def convert(self, s, nRows):
            if nRows == 1:
                return s
        
            array = ['']*nRows
            for i in range(len(s)):
                index = i%(2*nRows-2)
                if index < nRows:
                    array[index] += s[i]
                else: 
                    array[2*nRows - index- 2] += s[i]
                    
            out = ''
            for i in array:
                out = out + i 
            return out

Log in to reply
 

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