Easy intuitive python solution


  • 0
    T
    class Solution:
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if numRows == 1:
                return s
            ret = ['']*numRows
            curr_row = 0
            direction = -1
            for char in s:
                ret[curr_row]+=char
                if curr_row == numRows-1 or curr_row == 0:
                    direction*=-1
                curr_row = curr_row+direction
            ret_str = ''
            for row in ret:
                ret_str+=row
            return ret_str
    

Log in to reply
 

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