Simple Python solution by utilizing periodism of reminder

  • 0
    from operator import itemgetter
    class Solution(object):
        def convert(self, s, numRows):
            :type s: str
            :type numRows: int
            :rtype: str
            if numRows == 1 or numRows >= len(s):
                return s
            res, period = [], numRows - 1
            for (idx, ele) in enumerate(s):
                res.append((ele, abs(idx % (2 * period) - period)))
            res = sorted(res, key=itemgetter(1), reverse=True)
            return "".join([x[0] for x in res])

Log in to reply

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