LeetCode — Spiral Order

Given an m x n matrix, return all elements of the matrix in spiral order.

Example 1:

Example 2:

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

Solution: