首页 > 其他 > 详细

LeetCode 867 Transpose Matrix 解题报告

时间:2019-02-05 10:41:01      阅读:164      评论:0      收藏:0      [点我收藏+]

题目要求

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

                

        

 

LeetCode 867 Transpose Matrix 解题报告

原文:https://www.cnblogs.com/yao1996/p/10352518.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!