Notes

Personal notes on various topics

View on GitHub

Spiral Matrix

Problem Statement

Given an m x n matrix, return all elements of the matrix in spiral order.

Examples

Example 1:

spiral matrix example

Input:
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output:
[1,2,3,6,9,8,7,4,5]

Example 2:

spiral matrix example

Input:
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output:
[1,2,3,4,8,12,11,10,9,5,6,7]

Constraints

Code Template

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        # Your code here
        pass

Solutions

Back to Problem List Back to Categories