straight forward python O(n) solution


  • 0
    J
    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if numRows == 1:
                return s
            container = [[] for _ in range(numRows)]
            row = 0
            down = True
            up = False
            for i in range(len(s)):
                container[row].append(s[i])
                if row == 0:
                    down = True
                    up = False
                    row += 1
                elif row == numRows-1:
                    up = True
                    down = False
                    row -= 1
                elif row < numRows-1 and down:
                    row += 1
                elif row < numRows-1 and up:
                    row -= 1
            return ''.join([''.join(r) for r in container])
    

Log in to reply
 

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