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.
1 <= A.length <= 1000 1 <= A.length <= 1000
Time: O(R C) in A
Space: O(R C) in A’s transpose
Start by creating the result matrix which is made up of a list of lists; there will be
len(A) of them. Loop through each row and start appending each column value to the row. Finally return the rows. Alternatively, Python has this one liner solution:
What zip does is that if you have
[1,2,3],[4,5,6],[7,8,9] then it will create a result for each column as a list so you get the final transpose.
*A basically takes a list of lists and separates it out so you’re looking at
zip([1,2,3],[4,5,6],[7,8,9]). It’s pretty neat, but supposedly these kind of pythonic solutions are looked down upon in interviews, so try to avoid them. But good to have in your backpocket, just in case.