matrix
of size n x n
with all elements set to 0.num
to 1.top
, bottom
, left
, and right
.num
is less than or equal to n^2
, repeat the following steps:
a. Traverse from left
to right
along the top
row and fill the cells with num
.
b. Increment top
to move to the next inner layer.
c. Traverse from top
to bottom
along the right
column and fill the cells with num
.
d. Decrement right
to narrow the layer.
e. Traverse from right
to left
along the bottom
row and fill the cells with num
.
f. Decrement bottom
to move to the next inner layer.
g. Traverse from bottom
to top
along the left
column and fill the cells with num
.
h. Increment left
to narrow the layer.matrix
.