Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution(object):
- def convert(self, s, numRows):
- """
- :type s: str
- :type numRows: int
- :rtype: str
- """
- if numRows == 1:
- return s # No need to convert if there's only one row
- rows = [''] * numRows # Create empty rows to store the characters
- currentRow = 0
- goingDown = True
- for char in s:
- rows[currentRow] += char # Add the character to the current row
- if goingDown:
- currentRow += 1 # Move down if going down
- if currentRow == numRows - 1:
- goingDown = False # Change direction at the bottom
- else:
- currentRow -= 1 # Move up if going up
- if currentRow == 0:
- goingDown = True # Change direction at the top
- return ''.join(rows) # Concatenate the characters from all rows
Add Comment
Please, Sign In to add comment