LeetCode 867 Transpose Matrix 解题报告

题目要求

Given a matrix A, return the transpose of A.

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

题目分析及思路

题目要求得到矩阵的转置矩阵。可先得到一个行数与原矩阵列数相等、列数与原矩阵行数相等的矩阵,再对原矩阵进行遍历。

python代码​

class Solution:

    def transpose(self, A: 'List[List[int]]') -> 'List[List[int]]':

        rows, cols = len(A), len(A[0])

        res = [[0] * rows for _ in range(cols)]

        for row in range(rows):

            for col in range(cols):

                res[col][row] = A[row][col]

        return res

                

        

猜你喜欢

转载自www.cnblogs.com/yao1996/p/10352518.html