Transpose Matrix
Given a rectangular grid of numbers with m rows and n columns, return its transpose: a new grid with n rows and m columns in which the value at row i, column j of the input sits at row j, column i.
In other words, the grid is flipped over its main diagonal, so the first row becomes the first column, the second row the second column, and so on. The input is not modified; return the new grid.
Constraints
1 <= m, n <= 1000-10^9 <= matrix[i][j] <= 10^9
Examples
Example 1
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[1,4,7],[2,5,8],[3,6,9]]
A square grid is mirrored over its main diagonal: the first row becomes the first column.
Example 2
Input: matrix = [[1,2,3],[4,5,6]] Output: [[1,4],[2,5],[3,6]]
A 2 x 3 grid becomes a 3 x 2 grid. The result has a different shape from the input.
Example 3
Input: matrix = [[1],[2],[3]] Output: [[1,2,3]]
A single column turns into a single row.
JavaScript you'll use: Arrays: create, read, update
/**
* @param {number[][]} matrix
* @return {number[][]}
*/
function transpose(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.