Spiral Matrix

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

Solutions

The time complexity is still O(mn), and the space complexity is O(1) excluding the output. This is because it does not use any additional data structures whose size depends on the input. The output size is O(mn), as it includes all elements of the matrix. This version of the method

Last updated