@@ -15,34 +15,35 @@ def generate_matrix(self, n: int) -> list[list[int]]:
1515 # Continue filling the matrix layer by layer in spiral order
1616 while row_start <= row_end and col_start <= col_end :
1717 # Step 1: Fill the top row (left → right)
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-
18+ for i in range (col_start , col_end + 1 ):
19+ result [row_start ][i ] = value # assign the current value
20+ value += 1 # move to next number
21+ row_start += 1 # move top boundary down (row filled)
22+
2323 # Step 2: Fill the rightmost column (top → bottom)
2424 for row in range (row_start , row_end + 1 ):
2525 result [row ][col_end ] = value
2626 value += 1
27- col_end -= 1 # Move right boundary left (column filled)
28-
27+ col_end -= 1 # move right boundary left (column filled)
28+
2929 # Step 3: Fill the bottom row (right → left)
3030 # Only if there are rows remaining to fill
3131 if row_start <= row_end :
3232 for col in range (col_end , col_start - 1 , - 1 ):
3333 result [row_end ][col ] = value
3434 value += 1
35- row_end -= 1 # Move bottom boundary up (row filled)
36-
35+ row_end -= 1 # move bottom boundary up (row filled)
36+
3737 # Step 4: Fill the leftmost column (bottom → top)
3838 # Only if there are columns remaining to fill
3939 if col_start <= col_end :
4040 for row in range (row_end , row_start - 1 , - 1 ):
4141 result [row ][col_start ] = value
4242 value += 1
43- col_start += 1 # Move left boundary right (column filled)
43+
44+ col_start += 1
4445
45- # Return the completed spiral matrix
46+ # return the completed spiral matrix
4647 return result
4748
4849
@@ -56,7 +57,11 @@ def generate_matrix(self, n: int) -> list[list[int]]:
5657 for row in matrix :
5758 print (row )
5859
59- # Output:
60- # [1, 2, 3]
61- # [8, 9, 4]
62- # [7, 6, 5]
60+
61+ # Output:
62+ '''
63+ [1, 2, 3]
64+ [8, 9, 4]
65+ [7, 6, 5]
66+
67+ '''
0 commit comments