LeetCode 6. ZigZag Conversion

2018-11-14  本文已影响10人  费城的二鹏

ZigZag Conversion

class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        print(s)

        if numRows <= 1:
            return s
        
        # rows = []
        # for i in range(numRows):
        #     rows.append("")
        rows = [""] * numRows

        row = 0
        dir = 1

        for i in range(len(s)):
            if row == 0:
                dir = 1
            elif row == numRows - 1:
                dir = -1
            
            rows[row] = rows[row] + s[i]
            row = row + dir

        # result = ""
        # for i in range(numRows):
        #     result = result + rows[i]

        result = ''.join(rows)
        
        print(result)
        return result

上一篇下一篇

猜你喜欢

热点阅读