
g0001_0100.s0006_zigzag_conversion.Solution.py Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
# #Medium #String #2024_06_03_Time_41_ms_(94.82%)_Space_16.5_MB_(92.96%)
class Solution:
def convert(self, s: str, numRows: int) -> str:
s_len = len(s)
if numRows == 1:
return s
max_dist = numRows * 2 - 2
buf = []
for i in range(numRows):
index = i
if i == 0 or i == numRows - 1:
while index < s_len:
buf.append(s[index])
index += max_dist
else:
while index < s_len:
buf.append(s[index])
index += max_dist - i * 2
if index >= s_len:
break
buf.append(s[index])
index += i * 2
return ''.join(buf)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy