Skip to content
BASA DSA

Spiral Matrix

MediumMath & Geometry

Return matrix entries in clockwise spiral order, beginning at the top-left and first moving right.

Example 1

0,01
0,12
0,23
1,04
1,15
1,26
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
matrix = [[1,2,3],[4,5,6]]
Output
[1,2,3,6,5,4]

Explanation

Read top row 1,2,3, right edge 6, then bottom row backwards 5,4.

Example 2

0,01
1,02
2,03
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
matrix = [[1],[2],[3]]
Output
[1,2,3]

Explanation

Read the single column downwards without revisiting cells.

Example 3

0,07
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
matrix = [[7]]
Output
[7]

Explanation

The only cell is emitted once.

Constraints

  • 1 <= rows,columns <= 10
  • Entries are signed integers

Function contractspiralOrderArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…