Spiral Matrix

Given an m × n grid matrix, return all of its numbers in spiral order: along the top row to the right, down the right column, back along the bottom row to the left, up the left column, and then the same again one ring further in, until every number has been read once.

Constraints

  • 1 ≤ m, n ≤ 100
  • -100 ≤ matrix[r][c] ≤ 100

Example 1

Input
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output
[1,2,3,6,9,8,7,4,5]
Why:

Around the edge clockwise, then the 5 in the middle.

Example 2

Input
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output
[1,2,3,4,8,12,11,10,9,5,6,7]
Why:

The inner ring is the single row 6, 7.

Hints

    Was this a good challenge?

    Solved by
    0
    Submissions
    0
    Accepted
    –