Spiral Matrix
You get a matrix of rows × columns numbers. Return all of them in spiral order: along the top row to the right, down the right column, back along the bottom row, up the left column, and then the same again on the ring inside, until every number is taken once. Our example: 3 × 5, [1, 2, 3, 4, 5], [12, 13, 14, 15, 6], [11, 10, 9, 8, 7]. It is numbered along the spiral, so the answer counts up: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].
- input
matrix:mrows ofnintegers.- returns
- All the numbers of
matrixin spiral order: from the top-left corner right along the top row, down the right column, left along the bottom row, up the left column, and so on inwards, clockwise. - constraints
- 1 ≤ m = matrix.length ≤ 10
- 1 ≤ n = matrix[i].length ≤ 10, the same for every row
- −100 ≤ matrix[i][j] ≤ 100
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[][]} matrix
* @return {number[]}
*/
function spiralOrder(matrix) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more matrix traversal problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.