알고리즘/LeetCode
6. Zigzag Conversion
앤테바
2022. 12. 7. 08:20
반응형
6. Zigzag Conversion
https://leetcode.com/problems/zigzag-conversion/description/
Zigzag Conversion - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제)
솔루션1)
numRows 수 만큼 배열을 생성해서 문자열을 순회하면서 배열에 아래로/위로 움직이면서 문자를 추가
class Solution:
def convert(self, s: str, numRows: int) -> str:
rets = [[] for i in range(numRows)]
idx = 0
# 0 = down, 1 = up
direction = 1
for c in s:
rets[idx].append(c)
if direction == 1:
idx += 1
if idx >= numRows:
idx -= 2
direction = 0
else:
idx -= 1
if idx < 0:
idx += 2
direction = 1
return ''.join([''.join(ret) for ret in rets])
반응형