Straight forward Python solution control the list index to increment in zigzag way


  • 0
    S
    class Solution(object):
        def convert(self, s, numRows):
            if numRows<=1:return s
            trans=['' for i in range(numRows)]
            j,incr=0,-1 #using incr to control list index increment in zigzag way
            for i in s:
                trans[j]+=i
                if j==0 or j==numRows-1:incr*=-1 #alter the direction of inedx increment
                j+=incr
            temp=''
            for j in trans:
                temp+=j
            return temp

Log in to reply
 

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