Spiral Matrix - aloalgo

Spiral Matrix

Medium

You are given an m x n matrix.

Your task is to return all elements of the matrix in spiral order, starting from the top-left corner and moving clockwise.

Example 1

Input
[
  [1]
]
Output
[1]
Explanation:

The matrix has only one element, so the spiral order is just [1].

Example 2

Input
[
  [1, 2],
  [3, 4]
]
Output
[1, 2, 4, 3]
Explanation:

Starting from the top-left (1), move right to (2), then down to (4), then left to (3).

Example 3

Input
[
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
Output
[1, 2, 3, 6, 9, 8, 7, 4, 5]
Explanation:

Starting from the top-left (1), move right to (2, 3), then down to (6, 9), then left to (8, 7), then up to (4), and finally right to (5).

Loading...
Input
[
  [1]
]
Output
[1]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!