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:
matrix = [ [0,1,2], [3,4,5], [6,7,8]]
Output:
[0,1,2,5,8,7,6,3,4]
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.
from collections import dequedef spiral_order(matrix): matrix = deque(deque(row) for row in matrix) result = [] while matrix: result += matrix.popleft() if matrix and matrix[0]: for row in matrix: result.append(row.pop()) if matrix: result += reversed(matrix.pop()) if matrix and matrix[0]: for row in reversed(matrix): result.append(row.popleft()) return result
public List<Integer> spiralOrder(List<List<Integer>> matrix) { List<Integer> result = new ArrayList<>(); while (!matrix.isEmpty()) { result.addAll(matrix.remove(0)); if (!matrix.isEmpty() && !matrix.get(0).isEmpty()) { for (List<Integer> row : matrix) { result.add(row.remove(row.size() - 1)); } } if (!matrix.isEmpty()) { List<Integer> lastRow = matrix.remove(matrix.size() - 1); Collections.reverse(lastRow); result.addAll(lastRow); } if (!matrix.isEmpty() && !matrix.get(0).isEmpty()) { for (int i = matrix.size() - 1; i >= 0; i--) { result.add(matrix.get(i).remove(0)); } } } return result;}
func spiralOrder(matrix [][]int) []int { var result []int for len(matrix) > 0 { result = append(result, matrix[0]...) matrix = matrix[1:] if len(matrix) > 0 && len(matrix[0]) > 0 { for i := range matrix { result = append(result, matrix[i][len(matrix[i])-1]) matrix[i] = matrix[i][:len(matrix[i])-1] } } if len(matrix) > 0 { lastRow := matrix[len(matrix)-1] matrix = matrix[:len(matrix)-1] for i := len(lastRow) - 1; i >= 0; i-- { result = append(result, lastRow[i]) } } if len(matrix) > 0 && len(matrix[0]) > 0 { for i := len(matrix) - 1; i >= 0; i-- { result = append(result, matrix[i][0]) matrix[i] = matrix[i][1:] } } } return result}
function spiralOrder(matrix: number[][]): number[] { let result: number[] = []; while (matrix.length > 0) { result = result.concat(matrix.shift()!); if (matrix.length > 0 && matrix[0].length > 0) { for (let row of matrix) { result.push(row.pop()!); } } if (matrix.length > 0) { result = result.concat(matrix.pop()!.reverse()); } if (matrix.length > 0 && matrix[0].length > 0) { for (let i = matrix.length - 1; i >= 0; i--) { result.push(matrix[i].shift()!); } } } return result;}
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.
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.
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.
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.