create the output without build the actual zigzag array


  • 0
    C
    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if numRows == 1:return s
            n = len(s)
            res = ""
            basediff = numRows*2 - 2
            flag = 1
            #curr = 0
            for i in range(numRows):
                curr = i
                prev = -1
                flag = 0
                while curr < n:
                    if curr != prev:
                        res += s[curr] 
                    prev = curr
                    diff = abs(flag*basediff - basediff + 2*i)
                    curr += diff
                    flag = 1 - flag
            return res
    

    no need to create the actual array, just some math calculation


Log in to reply
 

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