Leetcode学习笔记:#867. Transpose Matrix
Given a matrix A, return the transpose of A.
The transpose of a matrix is the matrix flipped over it’s main diagonal, switching the row and column indices of the matrix.
实现:
public int[][] tranpose(int[][] A){
int N = A.length, N = A[0].length;
int[][] B = new int[N][M];
for(int j = 0; j < N; j++){
for(int i = 0; i < M; i++){
B[j][i] = A[i][j];
}
}
return B;
}
思路:
遍历矩阵,每遇到一个位置的数字,把j和i调换加入B。