python leetcode 6. ZigZag Conversion

对字符串进行操作

有几行就先初始化几个tmp=’’
然后遍历字符串 找到每个字符应该存到第几行
顺序是从第一行到最后一行再到第一行到最后一行…如此反复

class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows == 1:
            return s 
        temp = ['' for i in range(numRows)]
        index= -1
        step = 1
        for i in range(len(s)):
            index +=step
            if index == numRows:
                index -=2 
                step = -1 
            if index == -1:
                index=1
                step=1
            temp[index] += s[i]
        return ''.join(temp)

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84799641