[LeetCode/Python] 6. Zigzag Conversion

도니·2025년 9월 28일

Interview-Prep

목록 보기
19/29
post-thumbnail

📌 Problem

[LeetCode] 6. Zigzag Conversion

📌 Solution

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows == 1 or numRows >= len(s):
            return s
        
        rows = [''] * numRows
        i, step = 0, 1  # current row index and direction (+1 down, -1 up)

        for ch in s:
            rows[i] += ch
            # flip direction at the top/bottom
            if i == 0:
                step = 1
            elif i == numRows - 1:
                step = -1
            i += step

        return ''.join(rows)
profile
Where there's a will, there's a way

0개의 댓글