Back to Main
Learn DSA
Depth-First Search
Greedy Algorithms
Get Premium
Matrices
Spiral Matrix
medium
DESCRIPTION (credit Leetcode.com)
Write a function to traverse an m x n matrix in spiral order and return all elements in a single list. The traversal should start from the top left corner and proceed clockwise, spiraling inward until every element has been visited.
Input:
Output:
Explanation: The elements of the matrix are returned in the order they are visited in a clockwise spiral starting from the top left corner.
Explanation
This solution uses 4 steps to traverse the matrix in spiral order:
1. Top Row
The first step is to pop the first row of the matrix, while copying those elements into the result array from left to right.
def spiral_order(matrix):result = []while matrix:result += matrix.pop(0)if matrix and matrix[0]:for row in matrix:result.append(row.pop())if matrix:result += matrix.pop()[::-1]if matrix and matrix[0]:for row in matrix[::-1]:result.append(row.pop(0))return result
initialize result
0 / 1
1x
2. Right Column
Next, we traverse the right column of the matrix, popping the last element of each remaining row in matrix and copying those elements into the result array from top to bottom.
def spiral_order(matrix):result = []while matrix:result += matrix.pop(0)if matrix and matrix[0]:for row in matrix:result.append(row.pop())if matrix:result += matrix.pop()[::-1]if matrix and matrix[0]:for row in matrix[::-1]:result.append(row.pop(0))return result
traverse top row
0 / 2
1x
3. Bottom Row
Next, we traverse the bottom row of the matrix, popping the last row of matrix and copying those elements into the result array from right to left.
def spiral_order(matrix):result = []while matrix:result += matrix.pop(0)if matrix and matrix[0]:for row in matrix:result.append(row.pop())if matrix:result += matrix.pop()[::-1]if matrix and matrix[0]:for row in matrix[::-1]:result.append(row.pop(0))return result
traverse right column
0 / 1
1x
4. Left Column
Finally, we traverse the left column of the matrix, popping the first element of each remaining row in matrix and copying those elements into the result array from bottom to top.
def spiral_order(matrix):result = []while matrix:result += matrix.pop(0)if matrix and matrix[0]:for row in matrix:result.append(row.pop())if matrix:result += matrix.pop()[::-1]if matrix and matrix[0]:for row in matrix[::-1]:result.append(row.pop(0))return result
traverse bottom row
0 / 1
1x
Repeat
At this point, we have traversed the perimeter of the original matrix in a clockwise spiral order. We repeat the process for the inner submatrix, until there are no more elements left to traverse.
def spiral_order(matrix):result = []while matrix:result += matrix.pop(0)if matrix and matrix[0]:for row in matrix:result.append(row.pop())if matrix:result += matrix.pop()[::-1]if matrix and matrix[0]:for row in matrix[::-1]:result.append(row.pop(0))return result
traverse left column
0 / 2
1x
Solution
def spiral_order(matrix):result = []while matrix:result += matrix.pop(0)if matrix and matrix[0]:for row in matrix:result.append(row.pop())if matrix:result += matrix.pop()[::-1]if matrix and matrix[0]:for row in matrix[::-1]:result.append(row.pop(0))return result
spiral matrix
0 / 8
1x
Complexity Analysis
- Time complexity: O(m * n) where m is the number of rows and n is the number of columns (we visit each element in the matrix once).
- Space complexity: O(m * n) for the result array.
Login to track your progress
Your account is free and you can post anonymously if you choose.