867. Transpose Matrix - LeetCode

Question

867. Transpose Matrix

Solution

题目大意:矩阵的转置

思路:定义一个转置后的二维数组,遍历原数组,在赋值时行号列号互换即可

Java实现:

public int[][] transpose(int[][] A) {
    int[][] B = new int[A[0].length][A.length];
    for (int i = 0; i < A.length; i++) {
        for (int j = 0; j < A[0].length; j++) {
            B[j][i] = A[i][j];
        }
    }
    return B;
}

猜你喜欢

转载自www.cnblogs.com/okokabcd/p/9463056.html