Description

Given a 2D integer array matrix, return the transpose of matrix. The transpose is the matrix flipped over its main diagonal, switching the row and column indices.

Examples

Input:matrix = [[1,2,3],[4,5,6]]
Output:[[1,4],[2,5],[3,6]]
Explanation:

Swap rows and columns.

Input:matrix = [[7,8],[9,10],[11,12]]
Output:[[7,9,11],[8,10,12]]
Explanation:

This 3x2 matrix becomes a 2x3 matrix. The first column [7,9,11] becomes the first row, and the second column [8,10,12] becomes the second row.

Input:matrix = [[5,10,15,20]]
Output:[[5],[10],[15],[20]]
Explanation:

This single-row matrix (1x4) becomes a single-column matrix (4x1). Each element in the original row becomes a separate row in the transposed matrix.

Constraints

  • 1 ≤ m, n ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!