LeetCode-867. Transpose Matrix

867. Transpose Matrix

Title description

Gives you a two-dimensional integer array matrix, and returns the transposed matrix of the matrix.

The transposition of a matrix refers to flipping the main diagonal of the matrix and exchanging the row index and column index of the matrix.
Insert picture description here

Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3 ,6,9]]
Example 2:

Input: matrix = [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]]

Problem-solving ideas

class Solution_867 {
    
    
    public int[][] transpose(int[][] matrix) {
    
    
        int i = matrix.length;
        int j = matrix[0].length;
        int[][] t = new int[j][i];
        for (int k = 0; k < i; k++) {
    
    
            for (int l = 0; l < j; l++) {
    
    
                t[l][k] = matrix[k][l];
            }
        }
        return t;
    }
}

Guess you like

Origin blog.csdn.net/qq_35655602/article/details/115256372