Skip to content

Commit ff578fb

Browse files
committed
fixing the typo
1 parent 0ea4a85 commit ff578fb

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed
Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,62 @@
11
class Solution:
22
def generate_matrix(self, n: int) -> list[list[int]]:
3-
# create an n x n matrix filled with zeros
3+
"""Generate an n x n spiral matrix filled with numbers from 1 to n^2."""
4+
5+
# Create an n x n matrix filled with zeros
46
result = [[0] * n for _ in range(n)]
5-
7+
68
# Start filling numbers from 1 to n^2
79
value = 1
8-
10+
911
# Define the boundaries for rows and columns
1012
row_start, row_end = 0, n - 1
1113
col_start, col_end = 0, n - 1
1214

1315
# Continue filling the matrix layer by layer in spiral order
1416
while row_start <= row_end and col_start <= col_end:
15-
1617
# Step 1: Fill the top row (left → right)
17-
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
20-
row_start += 1 # move top boundary down (row filled)
21-
18+
for col in range(col_start, col_end + 1):
19+
result[row_start][col] = value # Assign the current value
20+
value += 1 # Move to next number
21+
row_start += 1 # Move top boundary down (row filled)
22+
2223
# Step 2: Fill the rightmost column (top → bottom)
23-
for j in range(row_start, row_end + 1):
24-
result[j][col_end] = value
24+
for row in range(row_start, row_end + 1):
25+
result[row][col_end] = value
2526
value += 1
26-
col_end -= 1 # move right boundary left (column filled)
27-
27+
col_end -= 1 # Move right boundary left (column filled)
28+
2829
# Step 3: Fill the bottom row (right → left)
2930
# Only if there are rows remaining to fill
3031
if row_start <= row_end:
31-
for k in range(col_end, col_start - 1, -1):
32-
result[row_end][k] = value
32+
for col in range(col_end, col_start - 1, -1):
33+
result[row_end][col] = value
3334
value += 1
34-
row_end -= 1 # move bottom boundary up (row filled)
35-
35+
row_end -= 1 # Move bottom boundary up (row filled)
36+
3637
# Step 4: Fill the leftmost column (bottom → top)
3738
# Only if there are columns remaining to fill
3839
if col_start <= col_end:
39-
for l in range(row_end, row_start - 1, -1):
40-
result[l][col_start] = value
40+
for row in range(row_end, row_start - 1, -1):
41+
result[row][col_start] = value
4142
value += 1
42-
43-
col_start += 1
43+
col_start += 1 # Move left boundary right (column filled)
4444

45-
# return the completed spiral matrix
45+
# Return the completed spiral matrix
4646
return result
4747

4848

49-
# example usage
50-
solution = Solution()
51-
n = 3 # change this to any number, e.g., 4 or 5
52-
matrix = solution.generate_matrix(n)
53-
54-
# print the spiral matrix row by row
55-
for row in matrix:
56-
print(row)
57-
49+
# Example usage
50+
if __name__ == "__main__":
51+
solution = Solution()
52+
n = 3 # Change this to any number, e.g., 4 or 5
53+
matrix = solution.generate_matrix(n)
5854

59-
# Output:
60-
'''
61-
[1, 2, 3]
62-
[8, 9, 4]
63-
[7, 6, 5]
55+
# Print the spiral matrix row by row
56+
for row in matrix:
57+
print(row)
6458

65-
'''
59+
# Output:
60+
# [1, 2, 3]
61+
# [8, 9, 4]
62+
# [7, 6, 5]

0 commit comments

Comments
 (0)