[알고리즘] leetcode Zigzag Conversion

진실·2022년 11월 23일
0

알고리즘

목록 보기
19/22
post-custom-banner

https://leetcode.com/problems/zigzag-conversion/

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        answer = ""

        if numRows == 1:
            return s
        indices = [[] for _ in range(numRows)]
        unit = 2 * numRows - 2
        for i in range(len(s)):
            mod = i % unit
            if mod < numRows:
                indices[mod].append(s[i])
            else:
                indices[unit - mod].append(s[i])

        for row in indices:
            for word in row:
                answer += word

        return answer
profile
반갑습니다.
post-custom-banner

0개의 댓글