Easy to understand python solution beats 100%


  • 0
    S
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if numRows == 1:
                return s
            period = numRows + numRows - 2
            zigzags = [""] * numRows
            for i in range(len(s)):
                u = i % period
                if u <= numRows - 1:
                    row = u
                else:
                    row = period - u
                zigzags[row] += s[i]
            ret = ""
            for i in range(len(zigzags)):
                ret += zigzags[i]
            return ret
    

Log in to reply
 

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