Easy · Coordinate Swap
Understand → Visualise → Practice.
Return the transpose of a non-empty rectangular matrix.
The transpose of an r × c matrix is c × r, with new[j][i] = old[i][j]: old columns become new rows.
Create a cols × rows result.
For every (i, j), set new[j][i] = old[i][j].
In place swapping only works for square matrices.
O(rows × cols) time and O(rows × cols) space.
Swapping in place inside a rectangular matrix fails because the shape changes.
Read the full guide first: Matrices and 2-D Arrays.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.