Skip to content

Commit bc94796

Browse files
authored
Create transpose-matrix.py
1 parent c344f8c commit bc94796

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Python/transpose-matrix.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Time: O(r * c)
2+
# Space: O(1)
3+
4+
# Given a matrix A, return the transpose of A.
5+
#
6+
# The transpose of a matrix is the matrix flipped over it's main diagonal,
7+
# switching the row and column indices of the matrix.
8+
#
9+
# Example 1:
10+
#
11+
# Input: [[1,2,3],[4,5,6],[7,8,9]]
12+
# Output: [[1,4,7],[2,5,8],[3,6,9]]
13+
# Example 2:
14+
#
15+
# Input: [[1,2,3],[4,5,6]]
16+
# Output: [[1,4],[2,5],[3,6]]
17+
#
18+
# Note:
19+
# - 1 <= A.length <= 1000
20+
# - 1 <= A[0].length <= 1000
21+
22+
class Solution(object):
23+
def transpose(self, A):
24+
"""
25+
:type A: List[List[int]]
26+
:rtype: List[List[int]]
27+
"""
28+
result = [[None] * len(A) for _ in xrange(len(A[0]))]
29+
for r, row in enumerate(A):
30+
for c, val in enumerate(row):
31+
result[c][r] = val
32+
return result

0 commit comments

Comments
 (0)