Share my Python code


  • 0
    W
    class Solution:
        # @return a string
        def convert(self, s, nRows):
            if nRows==1:
                return s
            else:
                k=2*nRows-2
                l=len(s)
                sl=[]
                for i in range(l):
                    sl.append(s[i])
                if k-l%k>0:
                    for i in range(k-l%k):
                        sl.append(' ')
                l=len(sl)
                tr=[]
                for j in range(nRows):
                    for i in range(l/k):
                        if j==0:
                            tr.append(sl[i*k])
                        elif j>0 and j<(nRows-1):
                            tr.append(sl[i*k+j])
                            tr.append(sl[(i+1)*k-j])
                        else:
                            tr.append(sl[i*k+nRows-1])
    
                return ''.join(tr).replace(' ','')

Log in to reply
 

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