#59

Spiral Matrix II

pupil · 495 · lc medium +27 · verified · 74.7% accepted · 6,893 likes · top 85%

play →

Description

Construct and return an n x n matrix whose entries are integers 1 through n2 placed in clockwise spiral order.

Example 1:

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

Example 2:

Input: n = 1
Output: [[1]]

Code

1
2
3