Python Program-easy to understand O(n)


  • 0
    A

    There are two kinds of iteration: first round iteration and the following iterations. I take care of them separately.
    Use dictionary to store numRows of words and then convert into a list and print out as a string.

    def convert(s,numRows):
            d={}
            a=[]
            for i,c in enumerate(s):
                if i<numRows:
                    d['part'+str(i)]=c
                else:
                    quo,rem=divmod(i-1,numRows-1)
        
                    if not (quo)%2:
                        d['part'+str(rem+1)]+=c
                    elif (quo)%2:
                        d['part'+str(numRows-rem-2)]+=c
            for j in range(numRows):
                a.append(d['part'+str(j)])
            print ''.join(a)

Log in to reply
 

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