Problem · Matrix
Generate a Spiral Matrix
Learn this problemProblem statement
Given a positive integer n, create an n by n matrix containing the integers from 1 through n * n in clockwise spiral order.
Begin with 1 in the top-left cell and initially move right.
Function
generateMatrix(n: int) → int[][]Examples
Example 1
n = 3return = [[1,2,3],[8,9,4],[7,6,5]]The values wrap clockwise around the boundary before filling the center cell.
Example 2
n = 1return = [[1]]A one-cell matrix contains only 1.
Constraints
nis positive.n * nfits in a signed integer.