867. Transpose Matrix
Question
Given a 2D integer array
matrix
, return the transpose ofmatrix
.The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix’s row and column indices.
Solution
记录一个新数组res[][]。
双重循环,交换下标将matrix[i][j]复制到res[j][i],最后返回。
Code
1 | class Solution { |
867. Transpose Matrix