Issah45d

Spiral Solution

Nov 3rd, 2025
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. Spiral Problem Solution:
  2.  
  3. class Solution(object):
  4.     def spiralOrder(self, matrix):
  5.         return matrix and [*matrix.pop(0)] + self.spiralOrder([*zip(*matrix)][::-1])
  6.  
  7.         #[[123][456][789]] -> []
  8.  
  9.  
  10.         """
  11.        explaination:
  12.        the 'matrix and' makes sure the matric is not empty
  13.        matrix.pop(0) removes ther first row (we want to add it back in the next step)
  14.        [*matric.pop(0)] it makes an array without the first row (* in python turns an iterable into its constitutents)
  15.        zip(*matrix)it splits the matrix into its rows and then combines them together in dfifrent order
  16.        [::-1] reverses the amtric
  17.        'self.spiralOrder' does keeps on doing it until there are no more elements in the list
  18.        the + before the self.spiralorder adds back the first row. Like if it is [6,9,8,7,4,5], then it will add back the [1,2,3] to it
  19.        the 'matric and [*matrix.pop(0)]' makes sure that after removing a row, the matrix isnt empty
  20.        """
Advertisement
Add Comment
Please, Sign In to add comment