首页 > 其他 > 详细

867. Transpose Matrix

时间:2020-07-01 12:06:45      阅读:37      评论: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的坑 x = [[0] * n] * m 内层的字数组是同一个,改变[0][1]的同时[1][1] [2][1] ...[m][1]都被改了。因此不能这么初始化二维list。

class Solution(object):
    def transpose(self, A):
        """
        :type A: List[List[int]]
        :rtype: List[List[int]]
        """
        n = len(A)
        m = len(A[0])
        B = []
        for i in range(m):
            B.append([0] * n)
        for i in range(m):
            for j in range(n):
                B[i][j] = A[j][i]
        return B

 

867. Transpose Matrix

原文:https://www.cnblogs.com/whatyouthink/p/13218274.html

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