14 lines of python


  • 0
    L
    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if numRows==1:
                return s
            A=[[] for i in range(numRows)]
            B=list(range(numRows))+list(range(numRows-2,0,-1))
            r=2*numRows-2
            j=0
            for i,c in enumerate(s):
                i=i%r
                A[B[i]].append(c)
            res=[]
            for i in range(numRows):
                res.extend(A[i])
            return ''.join(res)
               
    

Log in to reply
 

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