Zigzag conversion
Here is the solution of python:
lass Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1 or numRows >= len(s):
return s
# Create an empty list to hold characters in each row
rows = [''] * numRows
index = 0
step = 1
# Iterate through the input string
for char in s:
rows[index] += char # Append the character to the corresponding row
if index == 0:
step = 1 # Move down to next row
elif index == numRows - 1:
step = -1 # Move up to previous row
index += step
# Join the characters in each row to get the final zigzag string
result = ''.join(rows)
return result