54. Spiral Matrix
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : October 24, 2024
Last updated : October 24, 2024
Related Topics : Array, Matrix, Simulation
Acceptance Rate : 53.03 %
indicies r, c 0, 0 -> 0, c -> r, c -> r, 0 -> 1, 0 -> 1, c-1 -> r-1, c Requires range for LEFT cause -1 in slice causes [] aka blank lists to be outputted even if valid. In other words, this following line doesn't work due to python syntax reasons... output.extend(matrix[row_max][col_max: col_min - 1 : -1])
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
direction = 'R' # R, L, U, D
col_min, col_max = 0, len(matrix[0]) - 1
row_min, row_max = 0, len(matrix) - 1
output = []
while col_min <= col_max and row_min <= row_max :
match direction :
case 'R' :
output.extend(matrix[row_min][col_min : col_max + 1])
row_min += 1
direction = 'D'
case 'L' :
output.extend(matrix[row_max][i] for i in range(col_max, col_min - 1, -1))
row_max -= 1
direction = 'U'
case 'U' :
output.extend([matrix[i][col_min] for i in range(row_max, row_min - 1, -1)])
col_min += 1
direction = 'R'
case 'D' :
output.extend([matrix[i][col_max] for i in range(row_min, row_max + 1)])
col_max -= 1
direction = 'L'
return output