Skip to content

Commit f3a3cd9

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

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

data_structures/arrays/spiral_matrix.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,36 @@ 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
rowStart, rowEnd = 0, n - 1
1111
colStart, colEnd = 0, n - 1
1212

1313
# Continue filling the matrix layer by layer in spiral order
1414
while rowStart <= rowEnd and colStart <= colEnd:
15-
1615
# Step 1: Fill the top row (left → right)
1716
for i in range(colStart, colEnd + 1):
18-
result[rowStart][i] = value # assign the current value
19-
value += 1 # move to next number
17+
result[rowStart][i] = value # assign the current value
18+
value += 1 # move to next number
2019
rowStart += 1 # move top boundary down (row filled)
21-
20+
2221
# Step 2: Fill the rightmost column (top → bottom)
2322
for j in range(rowStart, rowEnd + 1):
2423
result[j][colEnd] = value
2524
value += 1
2625
colEnd -= 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 rowStart <= rowEnd:
3130
for k in range(colEnd, colStart - 1, -1):
3231
result[rowEnd][k] = value
3332
value += 1
3433
rowEnd -= 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 colStart <= colEnd:
@@ -55,10 +54,10 @@ def generate_matrix(self, n: int) -> list[list[int]]:
5554
print(row)
5655

5756

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

0 commit comments

Comments
 (0)