题目链接:6. Z 字形变换
找规律。
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows <= 1 or numRows >= len(s):
return s
span, ret = (numRows - 1) * 2, []
for i in range(0, len(s), span):
ret.append(s[i])
for i in range(1, numRows - 1):
spans, direct, j = [span - 2 * i, 2 * i], 0, i
while j < len(s):
ret.append(s[j])
j += spans[direct]
direct = 1 - direct
for i in range(numRows - 1, len(s), span):
ret.append(s[i])
return ''.join(ret)