Itertools version of Python solution

  • 0

    Not elegant enough but within 179ms

    class Solution:
        # @return a string
        def convert(self, s, nRows):
            if nRows == 1:
                return s
            rows = [i for i in itertools.repeat('', nRows)]
            pattern = list(range(nRows))
            for row, current_char in itertools.izip(itertools.cycle(itertools.chain(pattern, pattern[::-1][1:][:-1])), itertools.count()):
                if current_char >= len(s):
                rows[row] += s[current_char]
            return ''.join(rows)

Log in to reply

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