Spiral Matrix

Challenge Difficulty: Hard | Estimated completion time: ~45 minutes

Given a matrix of m * n elements (m rows, n columns), return all elements of the matrix in spiral order.

Examples

spiralOrder([
  [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  [ 7, 8, 9 ]
])
output = [1, 2, 3, 6, 9, 8, 7, 4, 5]
 
spiralOrder([
  [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]