KEEP GOING
[python] LeetCode : zigzag-conversion (ceil(), join()) 본문
반응형
https://leetcode.com/problems/zigzag-conversion/
1. 성공한 코드
from math import ceil
class Solution:
def convert(self, s: str, numRows: int) -> str:
arr = [[] for _ in range(numRows)]
n = len(s)
gap = numRows*2 -2
p1 = 0
p2 = gap
if gap == 0:
return s
for _ in range(ceil(n/gap)):
for i in range(0, numRows):
if p1+i < n:
arr[i].append(s[p1+i])
p1 += gap
for i in range(numRows-2, 0, -1):
if p2-i < n:
arr[i].append(s[p2-i])
p2 += gap
result = ''
for i in range(numRows):
result += "".join(arr[i])
return result
2. 코드 개선
반응형
'code review > sql' 카테고리의 다른 글
Comments