Spiral Matrix
Given a grid with m rows and n columns, return all of its values in clockwise spiral order, starting at the top-left cell: go right along the top row, down the last column, left along the bottom row, up the first column, then continue the same way into the smaller grid that remains inside.
Constraints
1 <= m, n <= 10-100 <= matrix[i][j] <= 100
Examples
Example 1
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5]
Go right along the top, down the right edge, left along the bottom, up the left edge, then the single middle cell.
Example 2
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7]
In a 3 x 4 grid the last lap is a single row: 6, 7 are read once going right, not again going left.
Example 3
Input: matrix = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] Output: [1,2,3,6,9,12,11,10,7,4,5,8]
In a 4 x 3 grid the last lap is a single column of two cells.
JavaScript you'll use: Arrays: create, read, update
/**
* @param {number[][]} matrix
* @return {number[]}
*/
function spiralOrder(matrix) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.