Spiral Matrix
Given an m x n matrix, return all its elements in spiral order, starting from the top-left corner and moving right, down, left, and up, spiraling inward.
Open official problem prompt ↗Read out every element exactly once following the clockwise inward spiral.
Like peeling an onion or unwinding a coil: you strip off the outer ring, then the next ring in, until nothing is left.
- Input
- matrix = [[1,2,3],[4,5,6],[7,8,9]]
- Output
- [1,2,3,6,9,8,7,4,5]
- Why
- Walking the outer ring clockwise gives 1,2,3,6,9,8,7,4 and the lone center cell 5 finishes the spiral.
m == matrix.lengthn == matrix[i].length1 <= m, n <= 10-100 <= matrix[i][j] <= 100