问题描述:
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.
求转置矩阵
思路:
交换行和列
代码如下:
class Solution {
public int[][] transpose(int[][] matrix) {
int height=matrix.length;
int length=matrix[0].length;
int[][] ans=new int[length][height];
for(int i=0; i<height; i++){
for(int j=0; j<length; j++){
ans[j][i]=matrix[i][j];
}
}
return ans;
}
}
时间复杂度:O(mn)