给你一个二维整数数组 matrix, 返回 matrix 的 转置矩阵 。
矩阵的 转置 是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[1,4,7],[2,5,8],[3,6,9]]
输入:matrix = [[1,2,3],[4,5,6]]
输出:[[1,4],[2,5],[3,6]]
提示:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 1000
1 <= m * n <= 105
-109 <= matrix[i][j] <= 109
class Solution {
public int[][] transpose(int[][] A) {
int a=A.length;
int b=A[0].length;
int [][]result=new int[b][a];
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
result[j][i]=A[i][j];
}
}
return result;
}
}
作者:Dqarden
链接:https://leetcode-cn.com/problems/transpose-matrix/solution/zhuan-zhi-ju-zhen-ti-jie-by-dqarden-mfu3/
原文:https://www.cnblogs.com/Dqarden/p/14778459.html