ByteByteGo: Spiral Traversal
• 282 words • 2 min • updated
python
from typing import List
def spiral_matrix(matrix: List[List[int]]) -> List[int]:
ans = []
start = (0, 0)
end = (len(matrix), len(matrix[0]))
def debian_once(start, end):
for j in range(start[1], end[1]):
ans.append(matrix[start[0]][j])
for i in range(start[0] + 1, end[0] - 1):
ans.append(matrix[i][end[1] - 1])
if end[0] - 1 > start[0]:
for j in range(end[1] - 1, start[1] - 1, -1):
ans.append(matrix[end[0] - 1][j])
if end[1] - 1 > start[1]:
for i in range(end[0] - 1 - 1, start[1], -1):
ans.append(matrix[i][start[1]])
# while start[0] < end[0] and start[1] < end[1]:
# while all(start[i] < end[i] for i in range(len(start))):
while all(x < y for (x, y) in zip(start, end)):
debian_once(start, end)
start = tuple(el + 1 for el in start)
end = tuple(el - 1 for el in end)
return ansBeware of not doing <- (leftwards) and ^ (upwards) whenever there’s a single
column or a single row!
Or, changing the meaning of end by shifting one cell diagonally inwards
(inclusive):
python
from typing import List
def spiral_matrix(matrix: List[List[int]]) -> List[int]:
ans = []
start = (0, 0)
end = (len(matrix) - 1, len(matrix[0]) - 1)
def debian_once(start, end):
for j in range(start[1], end[1] + 1):
ans.append(matrix[start[0]][j])
for i in range(start[0] + 1, end[0]):
ans.append(matrix[i][end[1]])
if start[0] < end[0]:
for j in range(end[1], start[1] - 1, -1):
ans.append(matrix[end[0]][j])
if start[1] < end[1]:
for i in range(end[0] - 1, start[1], -1):
ans.append(matrix[i][start[1]])
# while start[0] <= end[0] and start[1] <= end[1]:
# while all(start[i] <= end[i] for i in range(len(start))):
while all(x <= y for (x, y) in zip(start, end)):
debian_once(start, end)
start = tuple(el + 1 for el in start)
end = tuple(el - 1 for el in end)
return ans