Transpose Matrix

Given a 2D integer array matrix, return the transpose of matrix.

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.

Example 1
1 2 3
4 5 6
7 8 9
Inputmatrix = [[1,2,3],[4,5,6],[7,8,9]]
Output[[1,4,7],[2,5,8],[3,6,9]]
The rows and columns are swapped so the first output row contains the first element from each original row, and so on.
Example 2
1 2 3
4 5 6
Inputmatrix = [[1,2,3],[4,5,6]]
Output[[1,4],[2,5],[3,6]]
The 2 by 3 matrix becomes a 3 by 2 matrix with rows and columns swapped.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 1000
  • 1 <= m * n <= 10^5
  • -10^9 <= matrix[i][j] <= 10^9

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate