Peeush Agarwal > Engineer. Learner. Builder.

I am a Machine Learning Engineer passionate about creating practical AI solutions using Machine Learning, NLP, Computer Vision, and Azure technologies. This space is where I document my projects, experiments, and insights as I grow in the world of data science.

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