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.
class Solution(object):
def transpose(self, A):
"""
:type A: List[List[int]]
:rtype: List[List[int]]
"""
r = len(A)
c = len(A[0])
transpose = []
for i in range(c):
row = []
for j in range(r):
row.append(A[j][i])
transpose.append(row)
return transpose
1 返回一个matrix的话,初始定义的时候定义成[]
2 理清思路,一个matrix的transpose,是交换row和column的坐标
3 先一个一个地append,再一行一行(row)地append
4 在一个一个append的时候,要理清思路:因为返回transpose每一个row,其实是transpose前的每一列
5 这里外层循环其实先按列做循环的。因为要把transpose前的列放到transpose的行里面去