版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/maotianyi941005/article/details/85004976
Given a matrix
A
, return the transpose ofA
.The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.
Example 1:
Input: [[1,2,3],[4,5,6],[7,8,9]] Output: [[1,4,7],[2,5,8],[3,6,9]]
Example 2:
Input: [[1,2,3],[4,5,6]] Output: [[1,4],[2,5],[3,6]]
转置矩阵
1 my solution (叉腰自豪脸,fast
矩阵从Amxn ->B nxm、
B的行数是A的列数,所以遍历A的列进行append
B的第i行是A的第i列,使用listcomp生成一个A的第i列的列表
class Solution:
def transpose(self, A):
"""
:type A: List[List[int]]
:rtype: List[List[int]]
"""
B = []
for i in range(len(A[0])):
B.append([l[i] for l in A])
return B
Runtime: 60 ms, faster than 98.75% of Python3