Skip to content

Commit 05a12dc

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 0ea4a85 commit 05a12dc

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

data_structures/arrays/spiral_matrix.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,43 @@ class Solution:
22
def generate_matrix(self, n: int) -> list[list[int]]:
33
# create an n x n matrix filled with zeros
44
result = [[0] * n for _ in range(n)]
5-
5+
66
# Start filling numbers from 1 to n^2
77
value = 1
8-
8+
99
# Define the boundaries for rows and columns
1010
row_start, row_end = 0, n - 1
1111
col_start, col_end = 0, n - 1
1212

1313
# Continue filling the matrix layer by layer in spiral order
1414
while row_start <= row_end and col_start <= col_end:
15-
1615
# Step 1: Fill the top row (left → right)
1716
for i in range(col_start, col_end + 1):
18-
result[row_start][i] = value # assign the current value
19-
value += 1 # move to next number
17+
result[row_start][i] = value # assign the current value
18+
value += 1 # move to next number
2019
row_start += 1 # move top boundary down (row filled)
21-
20+
2221
# Step 2: Fill the rightmost column (top → bottom)
2322
for j in range(row_start, row_end + 1):
2423
result[j][col_end] = value
2524
value += 1
2625
col_end -= 1 # move right boundary left (column filled)
27-
26+
2827
# Step 3: Fill the bottom row (right → left)
2928
# Only if there are rows remaining to fill
3029
if row_start <= row_end:
3130
for k in range(col_end, col_start - 1, -1):
3231
result[row_end][k] = value
3332
value += 1
3433
row_end -= 1 # move bottom boundary up (row filled)
35-
34+
3635
# Step 4: Fill the leftmost column (bottom → top)
3736
# Only if there are columns remaining to fill
3837
if col_start <= col_end:
3938
for l in range(row_end, row_start - 1, -1):
4039
result[l][col_start] = value
4140
value += 1
42-
41+
4342
col_start += 1
4443

4544
# return the completed spiral matrix
@@ -56,10 +55,10 @@ def generate_matrix(self, n: int) -> list[list[int]]:
5655
print(row)
5756

5857

59-
# Output:
60-
'''
58+
# Output:
59+
"""
6160
[1, 2, 3]
6261
[8, 9, 4]
6362
[7, 6, 5]
6463
65-
'''
64+
"""

0 commit comments

Comments
 (0)