[코테 풀이] Transpose Matrix

시내·2024년 7월 26일
0

Q_867) Transpose Matrix

출처 : https://leetcode.com/problems/transpose-matrix/

Given a 2D integer array matrix, return the transpose of matrix.

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.

class Solution {
    public int[][] transpose(int[][] matrix) {
        int[][] answer = new int[matrix[0].length][matrix.length];
        int a = 0, b = 0, c = 0, d = 0;
        while (d < matrix[0].length) {
            if (c == matrix.length) {
                a++;
                b = 0;
                c = 0;
                d++;
            } else {
                answer[a][b] = matrix[c][d];
                b++;
                c++;
            }
        }
        return answer;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글