6. Zigzag Conversion
https://leetcode.com/problems/zigzag-conversion/
Python
公式拿Excel觀察歸納出來的,有k欄(即題目的numRows),該字元的index為i,則:
- Time: O(N)
- Space: O(N)
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows < 2:
return s
mapper = [[] for i in range(numRows)]
for i, char in enumerate(s):
bucket = numRows - abs(i%(numRows*2-2)-numRows+1) - 1
mapper[bucket].append(char)
return ''.join([char for bucket in mapper for char in bucket])