Wednesday, December 19, 2018

867. Transpose Matrix

二刷 08/2022
Version #1 Iteration
Time O(MN)
Space O(1)
Runtime: 1 ms, faster than 71.40% of Java online submissions for Transpose Matrix.
Memory Usage: 49.1 MB, less than 7.81% of Java online submissions for Transpose Matrix.
class Solution {
    public int[][] transpose(int[][] matrix) {
        int[][] result = new int[matrix[0].length][matrix.length];
        for (int y = 0; y < matrix.length; y++) {
            for (int x = 0; x < matrix[0].length; x++) {
                result[x][y] = matrix[y][x];
            }
        }
        return result;
    }
}


一刷
Straightforward

53.97 %
class Solution {
    public int[][] transpose(int[][] A) {
int[][] result = new int[A[0].length][A.length];
for (int y = 0; y < A.length; y++) {
for (int x = 0; x < A[0].length; x++) {
result[x][y] = A[y][x];
}
}
return result;
}
}

No comments:

Post a Comment