Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Spiral Problem Solution:
- class Solution(object):
- def spiralOrder(self, matrix):
- return matrix and [*matrix.pop(0)] + self.spiralOrder([*zip(*matrix)][::-1])
- #[[123][456][789]] -> []
- """
- explaination:
- the 'matrix and' makes sure the matric is not empty
- matrix.pop(0) removes ther first row (we want to add it back in the next step)
- [*matric.pop(0)] it makes an array without the first row (* in python turns an iterable into its constitutents)
- zip(*matrix)it splits the matrix into its rows and then combines them together in dfifrent order
- [::-1] reverses the amtric
- 'self.spiralOrder' does keeps on doing it until there are no more elements in the list
- 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
- the 'matric and [*matrix.pop(0)]' makes sure that after removing a row, the matrix isnt empty
- """
Advertisement
Add Comment
Please, Sign In to add comment