@@ -7,38 +7,39 @@ def generate_matrix(self, n: int) -> list[list[int]]:
77 value = 1
88
99 # Define the boundaries for rows and columns
10- rowStart , rowEnd = 0 , n - 1
11- colStart , colEnd = 0 , n - 1
10+ row_start , row_end = 0 , n - 1
11+ col_start , col_end = 0 , n - 1
1212
1313 # Continue filling the matrix layer by layer in spiral order
14- while rowStart <= rowEnd and colStart <= colEnd :
14+ while row_start <= row_end and col_start <= col_end :
1515
1616 # Step 1: Fill the top row (left → right)
17- for i in range (colStart , colEnd + 1 ):
18- result [rowStart ][i ] = value # assign the current value
17+ for i in range (col_start , col_end + 1 ):
18+ result [row_start ][i ] = value # assign the current value
1919 value += 1 # move to next number
20- rowStart += 1 # move top boundary down (row filled)
20+ row_start += 1 # move top boundary down (row filled)
2121
2222 # Step 2: Fill the rightmost column (top → bottom)
23- for j in range (rowStart , rowEnd + 1 ):
24- result [j ][colEnd ] = value
23+ for j in range (row_start , row_end + 1 ):
24+ result [j ][col_end ] = value
2525 value += 1
26- colEnd -= 1 # move right boundary left (column filled)
26+ col_end -= 1 # move right boundary left (column filled)
2727
2828 # Step 3: Fill the bottom row (right → left)
2929 # Only if there are rows remaining to fill
30- if rowStart <= rowEnd :
31- for k in range (colEnd , colStart - 1 , - 1 ):
32- result [rowEnd ][k ] = value
30+ if row_start <= row_end :
31+ for k in range (col_end , col_start - 1 , - 1 ):
32+ result [row_end ][k ] = value
3333 value += 1
34- rowEnd -= 1 # move bottom boundary up (row filled)
34+ row_end -= 1 # move bottom boundary up (row filled)
3535
3636 # Step 4: Fill the leftmost column (bottom → top)
3737 # Only if there are columns remaining to fill
38- if colStart <= colEnd :
39- for l in range (rowEnd , rowStart - 1 , - 1 ):
40- result [l ][colStart ] = value
38+ if col_start <= col_end :
39+ for l in range (row_end , row_start - 1 , - 1 ):
40+ result [l ][col_start ] = value
4141 value += 1
42+
4243 col_start += 1
4344
4445 # return the completed spiral matrix
0 commit comments