Spiral Matrix
Given an m x n matrix, return all elements in spiral order as a list.
The tests print the returned list space-separated.
Example
1,2,3 4,5,6 7,8,9
1 2 3 6 9 8 7 4 5
- The input is a grid written row by row:
1,2,3is the top row,4,5,6the middle row and7,8,9the bottom row. Spiral order walks the outer border clockwise from the top-left corner, then repeats on whatever is left inside. - Outer ring: the top edge, left to right, gives 1, 2, 3. The right edge going down adds 6, 9. The bottom edge, right to left, adds 8, 7. The left edge going up adds only 4, because 7 and 1 are corners already visited.
- That ring covers cells, each exactly once.
- What remains is a inner grid holding 5, which comes last:
1 2 3 6 9 8 7 4 5. - Notice that for a non-square grid the inner remainder can be a single row: the test finishes its outer ring at 5 and then reads the leftover row 6, 7 left to right.
Constraints
- 1 <= m, n <= 10
- -100 <= matrix[i][j] <= 100