Simple Python Solution


  • 1
    L

    Basically add each char to array in zigzagging pattern.

    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if numRows == 1 or len(s) < 2: return s
            rows = ["" for _ in range(numRows)]
            i, right = -1, True
            for ch in s:
                i = i + 1 if right else i - 1
                rows[i] += ch
                right = False if i == numRows-1 else True if i == 0 else right
            return "".join(rows)
    

Log in to reply
 

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