Problem
Spiral Matrix
Medium- matrix
matrix has m rows and n columns. Read its values in a clockwise spiral and return them as one flat list. Start at the top-left cell and walk right along the top row, down the last column, left along the bottom row and up the first column. Then repeat the same walk on the block that is left inside, until every value has been read once.
- Input
matrix = [[8, 2, 6], [1, 9, 4], [7, 3, 5]]- Output
[8, 2, 6, 4, 5, 3, 7, 1, 9]- Explanation
the outer ring gives
8, 2, 6, 4, 5, 3, 7, 1, and the center9is what remains.
- Input
matrix = [[5, 1, 9], [2, 8, 4], [7, 3, 6], [0, 11, 10]]- Output
[5, 1, 9, 4, 6, 10, 11, 0, 7, 2, 8, 3]- Explanation
the grid has more rows than columns. The outer ring gives
5, 1, 9, 4, 6, 10, 11, 0, 7, 2. Two cells are left inside,8and3, and they are read from top to bottom.
- Input
matrix = [[4], [8], [15]]- Output
[4, 8, 15]- Explanation
with a single column there is no ring to go around, so the walk only goes down it.
Constraints:
- every row of
matrixhas the same length
Tab indents. Press Esc, then Tab to leave the editor.
Run your code to see every test here. Nothing is submitted or recorded.