|
1 | 1 | class Solution: |
2 | 2 | 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 |
4 | 6 | result = [[0] * n for _ in range(n)] |
5 | | - |
| 7 | + |
6 | 8 | # Start filling numbers from 1 to n^2 |
7 | 9 | value = 1 |
8 | | - |
| 10 | + |
9 | 11 | # Define the boundaries for rows and columns |
10 | 12 | row_start, row_end = 0, n - 1 |
11 | 13 | col_start, col_end = 0, n - 1 |
12 | 14 |
|
13 | 15 | # Continue filling the matrix layer by layer in spiral order |
14 | 16 | while row_start <= row_end and col_start <= col_end: |
15 | | - |
16 | 17 | # 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 | + |
22 | 23 | # 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 |
25 | 26 | value += 1 |
26 | | - col_end -= 1 # move right boundary left (column filled) |
27 | | - |
| 27 | + col_end -= 1 # Move right boundary left (column filled) |
| 28 | + |
28 | 29 | # Step 3: Fill the bottom row (right → left) |
29 | 30 | # Only if there are rows remaining to fill |
30 | 31 | 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 |
33 | 34 | value += 1 |
34 | | - row_end -= 1 # move bottom boundary up (row filled) |
35 | | - |
| 35 | + row_end -= 1 # Move bottom boundary up (row filled) |
| 36 | + |
36 | 37 | # Step 4: Fill the leftmost column (bottom → top) |
37 | 38 | # Only if there are columns remaining to fill |
38 | 39 | 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 |
41 | 42 | value += 1 |
42 | | - |
43 | | - col_start += 1 |
| 43 | + col_start += 1 # Move left boundary right (column filled) |
44 | 44 |
|
45 | | - # return the completed spiral matrix |
| 45 | + # Return the completed spiral matrix |
46 | 46 | return result |
47 | 47 |
|
48 | 48 |
|
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) |
58 | 54 |
|
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) |
64 | 58 |
|
65 | | -''' |
| 59 | + # Output: |
| 60 | + # [1, 2, 3] |
| 61 | + # [8, 9, 4] |
| 62 | + # [7, 6, 5] |
0 commit comments