Advertisement
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 or numRows >= len(s):
- return s
- # Create a list of strings to store characters in each row
- rows = [''] * numRows
- # Initialize variables for tracking the current row and direction
- current_row = 0
- direction = 1
- # Iterate through each character in the string
- for char in s:
- # Append the character to the current row
- rows[current_row] += char
- # Update the current row and direction
- current_row += direction
- # Change direction when reaching the top or bottom row
- if current_row == 0 or current_row == numRows - 1:
- direction *= -1
- # Concatenate rows to form the zigzag pattern
- return ''.join(rows)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement