Back to course home
0% completed
Vote For New Content
Spiral Matrix (medium)
Problem Statement
Given a 2D matrix of size m x n
, return the 1D array containing all elements of matrix in spiral order
.
Examples
Example 1:
- Input: matrix =
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]]
- Expected Output:
[1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]
- Justification: We have traversed the matrix in the spiral order.
Example 2:
- Input: matrix =
[[10,20,30],
[40,50,60],
[70,80,90]]
- Expected Output:
[10,20,30,60,90,80,70,40,50]
- Justification: The traversal starts at the top-left, moves right to the end of the row, then down the right column, then left along the bottom row, up the left column, and finally captures the center.
Example 3:
- Input: matrix =
[[1,2],
[3,4],
[5,6]]
- Expected Output:
[1,2,4,6,5,3]
- Justification: The traversal starts at the top-left, moves right, then down through the right column, and finally moves up the left column.
Constraints:
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 10
- -100 <= matrix[i][j] <= 100
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible