Guest User

Zigzag - Gemini

a guest
Feb 16th, 2024
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | Software | 0 0
  1. class Solution(object):
  2.     def convert(self, s, numRows):
  3.         """
  4.        :type s: str
  5.        :type numRows: int
  6.        :rtype: str
  7.        """
  8.         if numRows == 1:
  9.          return s  # No need to convert if there's only one row
  10.  
  11.         rows = [''] * numRows  # Create empty rows to store the characters
  12.         currentRow = 0
  13.         goingDown = True
  14.  
  15.         for char in s:
  16.             rows[currentRow] += char  # Add the character to the current row
  17.             if goingDown:
  18.                 currentRow += 1  # Move down if going down
  19.                 if currentRow == numRows - 1:
  20.                     goingDown = False  # Change direction at the bottom
  21.             else:
  22.                 currentRow -= 1  # Move up if going up
  23.                 if currentRow == 0:
  24.                     goingDown = True  # Change direction at the top
  25.  
  26.         return ''.join(rows)  # Concatenate the characters from all rows
  27.        
Add Comment
Please, Sign In to add comment