Problem Solving

99. Transpose Matrix

굥깡 2023. 1. 19. 17:58
728x90

https://leetcode.com/problems/transpose-matrix/description/

 

Transpose Matrix - LeetCode

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. [https://assets.leetcode.com/uploads/2021/02/10/hint_

leetcode.com

matrix를 transpose 하는 문제

class Solution:
    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
        new = [[] * len(matrix) for i in range(len(matrix[0]))]
        for idx, line in enumerate(matrix):
            for idx2, j in enumerate(line):
                new[idx2].append(j)
        return new

선형대수학 생각이 났다ㅎㅋ

원래는 swap 하는 방식으로 풀려고 했지만 그렇게 풀면 더 어렵다