@@ -2,37 +2,36 @@ class Solution:
22 def generateMatrix (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 :
@@ -56,10 +55,10 @@ def generateMatrix(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