Transpose 2D Matrix
MediumData EngineeringMatrixArray
Description
Given a 2D array matrix with r rows and c columns, return its transpose (c rows and r columns).
Examples
Input:
matrix = [[1,2,3],[4,5,6]]Output:
[[1,4],[2,5],[3,6]]Explanation:
Swapping rows and columns yields [[1,4],[2,5],[3,6]].
Input:
matrix = [[1]]Output:
[[1]]Explanation:
Swapping rows and columns yields [[1]].
Input:
matrix = [[1,2],[3,4]]Output:
[[1,3],[2,4]]Explanation:
Swapping rows and columns yields [[1,3],[2,4]].
Constraints
- •
1 ≤ r, c ≤ 100