Given a matrix
of size m
x n
, write a function to return the elements of the matrix arranged in a spiral order.
matrix: number[][]
: A 2D array matrix
of size m
x n
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]Output: [1,2,3,6,9,8,7,4,5]Explanation: Starting from the top-left corner, move right across the first row, then go down across the last column, then move left along the bottom row, and travel up in the first column. Continue this pattern, spiraling inward, until all the elements have been traversed.
Input: matrix = [[7,7,0,7],[4,3,7,2],[9,8,6,0],[6,1,3,6]]Output: [7,7,0,7,2,0,6,3,1,6,9,4,3,7,6,8]Explanation: Starting from the top-left corner, move right across the first row, then go down in the last column, move left along the bottom row, and travel up in the first column. Continue this pattern, spiraling inward, until all the elements have been traversed.
Input: matrix = [[1,2],[3,4]]Output: [1,2,4,3]Explanation: Starting from the top-left corner, move right across the first row, then go down in the last column, and finally move left across the bottom row.
m
, n
<= 100matrix[row][col]
<= 1,000Given a matrix
of size m
x n
, write a function to return the elements of the matrix arranged in a spiral order.
matrix: number[][]
: A 2D array matrix
of size m
x n
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]Output: [1,2,3,6,9,8,7,4,5]Explanation: Starting from the top-left corner, move right across the first row, then go down across the last column, then move left along the bottom row, and travel up in the first column. Continue this pattern, spiraling inward, until all the elements have been traversed.
Input: matrix = [[7,7,0,7],[4,3,7,2],[9,8,6,0],[6,1,3,6]]Output: [7,7,0,7,2,0,6,3,1,6,9,4,3,7,6,8]Explanation: Starting from the top-left corner, move right across the first row, then go down in the last column, move left along the bottom row, and travel up in the first column. Continue this pattern, spiraling inward, until all the elements have been traversed.
Input: matrix = [[1,2],[3,4]]Output: [1,2,4,3]Explanation: Starting from the top-left corner, move right across the first row, then go down in the last column, and finally move left across the bottom row.
m
, n
<= 100matrix[row][col]
<= 1,000console.log()
aparecerão aqui.