Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 791 Bytes

6.md

File metadata and controls

42 lines (31 loc) · 791 Bytes

ZigZag Conversion

Description

link


Solution

  • ignore mathmatic things and just use rows to record strings
  • turn around when index get to the border

Code

Complexity T : O(n) M : O(n)

class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows == 1 or numRows > len(s):
            return s
        
        rows = [''] * numRows
        index, step = 0, 1
        for x in s:
            rows[index] += x
            if index == 0:
                step = 1
            elif index == numRows - 1:
                step = -1
            index += step
        
        return ''.join(rows)