Python short code for reference.

  • 1
    def convert(self, s, numRows):
        if numRows == 1:
            return s
        res, gap = "", 2*(numRows-1)
        for i in xrange(numRows):
            tmp = i
            while tmp < len(s):
                res += s[tmp]
                # special cases 
                if i != 0 and i != numRows-1: 
                    if tmp+gap-2*i < len(s):
                        res += s[tmp+gap-2*i]
                tmp += gap
        return res

Log in to reply

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