LeetCode #6 – ZigZag Conversion [字符串]


The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.


class Solution:
    def convert(self, s, numRows):
        if numRows == 1: return s
        v = ['' for i in range(numRows)]
        cur, dir, res = 0, -1, ''
        for c in s:
            if cur == 0 or cur == numRows - 1:
                dir = -dir
            v[cur] += c
            cur += dir
        for i in v: res += i
        return res