Skip to content

Commit ff14247

Browse files
committed
fixing the code formating mistake and update the code
1 parent 5ff2bb3 commit ff14247

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11
class Solution:
2-
def generateMatrix(self, n: int) -> list[list[int]]:
3-
# Create an n x n matrix filled with zeros
2+
def generate_matrix(self, n: int) -> list[list[int]]:
3+
# 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+
1516
# Step 1: Fill the top row (left → right)
1617
for i in range(colStart, colEnd + 1):
17-
result[rowStart][i] = value # assign the current value
18-
value += 1 # move to next number
18+
result[rowStart][i] = value # assign the current value
19+
value += 1 # move to next number
1920
rowStart += 1 # move top boundary down (row filled)
20-
21+
2122
# Step 2: Fill the rightmost column (top → bottom)
2223
for j in range(rowStart, rowEnd + 1):
2324
result[j][colEnd] = value
2425
value += 1
2526
colEnd -= 1 # move right boundary left (column filled)
26-
27+
2728
# Step 3: Fill the bottom row (right → left)
2829
# Only if there are rows remaining to fill
2930
if rowStart <= rowEnd:
3031
for k in range(colEnd, colStart - 1, -1):
3132
result[rowEnd][k] = value
3233
value += 1
3334
rowEnd -= 1 # move bottom boundary up (row filled)
34-
35+
3536
# Step 4: Fill the leftmost column (bottom → top)
3637
# Only if there are columns remaining to fill
3738
if colStart <= colEnd:
3839
for l in range(rowEnd, rowStart - 1, -1):
3940
result[l][colStart] = value
4041
value += 1
41-
colStart += 1 # move left boundary right (column filled)
42+
col_start += 1
4243

43-
# Return the completed spiral matrix
44+
# return the completed spiral matrix
4445
return result
4546

4647

47-
# Example usage:
48+
# example usage
4849
solution = Solution()
50+
n = 3 # change this to any number, e.g., 4 or 5
51+
matrix = solution.generate_matrix(n)
4952

50-
n = 3 # You can change this to any number, e.g. 4 or 5
51-
matrix = solution.generateMatrix(n)
52-
53-
# Print the spiral matrix row by row
53+
# print the spiral matrix row by row
5454
for row in matrix:
5555
print(row)
5656

5757

58-
# Output:
59-
"""
58+
# Output:
59+
'''
6060
[1, 2, 3]
6161
[8, 9, 4]
6262
[7, 6, 5]
6363
64-
"""
64+
'''

0 commit comments

Comments
 (0)