-
- Notifications
You must be signed in to change notification settings - Fork 48.8k
Spiral matrix #13523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Spiral matrix #13523
Changes from 5 commits
4fe3771
a387f8e
bdf0001
5ff2bb3
ff14247
f3a3cd9
e6f8b27
b4e0e94
0ea4a85
05a12dc
ff578fb
51f3bbb
b351094
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
class Solution: | ||
def generate_matrix(self, n: int) -> list[list[int]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: | ||
# create an n x n matrix filled with zeros | ||
result = [[0] * n for _ in range(n)] | ||
| ||
# Start filling numbers from 1 to n^2 | ||
value = 1 | ||
| ||
# Define the boundaries for rows and columns | ||
rowStart, rowEnd = 0, n - 1 | ||
Check failure on line 10 in data_structures/arrays/spiral_matrix.py | ||
| ||
colStart, colEnd = 0, n - 1 | ||
Check failure on line 11 in data_structures/arrays/spiral_matrix.py | ||
| ||
| ||
# Continue filling the matrix layer by layer in spiral order | ||
while rowStart <= rowEnd and colStart <= colEnd: | ||
| ||
# Step 1: Fill the top row (left → right) | ||
for i in range(colStart, colEnd + 1): | ||
result[rowStart][i] = value # assign the current value | ||
value += 1 # move to next number | ||
rowStart += 1 # move top boundary down (row filled) | ||
| ||
# Step 2: Fill the rightmost column (top → bottom) | ||
for j in range(rowStart, rowEnd + 1): | ||
result[j][colEnd] = value | ||
value += 1 | ||
colEnd -= 1 # move right boundary left (column filled) | ||
| ||
# Step 3: Fill the bottom row (right → left) | ||
# Only if there are rows remaining to fill | ||
if rowStart <= rowEnd: | ||
for k in range(colEnd, colStart - 1, -1): | ||
result[rowEnd][k] = value | ||
value += 1 | ||
rowEnd -= 1 # move bottom boundary up (row filled) | ||
| ||
# Step 4: Fill the leftmost column (bottom → top) | ||
# Only if there are columns remaining to fill | ||
if colStart <= colEnd: | ||
for l in range(rowEnd, rowStart - 1, -1): | ||
result[l][colStart] = value | ||
value += 1 | ||
col_start += 1 | ||
| ||
# return the completed spiral matrix | ||
return result | ||
| ||
| ||
# example usage | ||
solution = Solution() | ||
n = 3 # change this to any number, e.g., 4 or 5 | ||
matrix = solution.generate_matrix(n) | ||
| ||
# print the spiral matrix row by row | ||
for row in matrix: | ||
print(row) | ||
| ||
| ||
# Output: | ||
''' | ||
[1, 2, 3] | ||
[8, 9, 4] | ||
[7, 6, 5] | ||
| ||
''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
data_structures/arrays/spiral_matrix.py
, please provide doctest for the functiongenerate_matrix
Please provide descriptive name for the parameter:
n