Problem
Problem page: https://leetcode.com/problems/spiral-matrix-ii/
Topics: Array
Difficulty: Medium
Similar problems: (M) Spiral Matrix
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n =3,You should return the following matrix:
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
Analysis
As in (M) Spiral Matrix, I use two nested for loops. Instead of outputting the elements in spiral order, I fill a two-dimensional array with the elements in spiral order.
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-spiral-matrix-ii/
