Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 595 Bytes

README.md

File metadata and controls

31 lines (25 loc) · 595 Bytes

Transpose Matrix

Solution 1

/**
 * Question   : 867. Transpose Matrix
 * Complexity : Time: O(m*n) ; Space: O(m*n)
 * Topics     : Array, Matrix
 */
class Solution {
    public int[][] transpose(int[][] matrix) {
        if (matrix == null || matrix.length == 0) {
            return matrix;
        }

        int m = matrix.length;
        int n = matrix[0].length;

        int[][] t = new int[n][m];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                t[j][i] = matrix[i][j];
            }
        }

        return t;
    }
}