Python, LeetCode, 6. Z字形变换

class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        N = len(s)
        res = ''
        if numRows == 1:
            return s
        for i in range(numRows):
            tmp0 = i
            while tmp0 < N:
                res += s[tmp0]
                tmp1 = tmp0 + (numRows - i - 1) * 2
                tmp2 = tmp1 + i * 2
                if tmp1 == tmp2:
                    tmp0 = tmp1
                elif tmp0 == tmp1:
                    tmp0 = tmp2
                else:
                    if tmp1 < N:
                        res += s[tmp1]
                    else:
                        break
                    tmp0 = tmp2
        return res

你可能感兴趣的:(LeetCode)