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的坑 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
原文地址:https://www.cnblogs.com/whatyouthink/p/13218274.html