zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Mybatis入门
    Ajax
    产品经理之产品规划
    产品经理之用户研究(下)
    产品经理之用户研究(上)
    Spring Cloud
    Spring MVC
    synchronized
    Spring Boot入门
    Spring
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13218274.html
Copyright © 2011-2022 走看看