Skip to content

Commit 61f2974

Browse files
Add solution to LC867
1 parent 4f9f40f commit 61f2974

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

LC867-Transpose-Matrix.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Given a matrix A, return the transpose of A.
3+
4+
The transpose of a matrix is the matrix flipped over it's main diagonal,
5+
switching the row and column indices of the matrix.
6+
7+
Example 1:
8+
Input: [[1,2,3],[4,5,6],[7,8,9]]
9+
Output: [[1,4,7],[2,5,8],[3,6,9]]
10+
11+
Example 2:
12+
Input: [[1,2,3],[4,5,6]]
13+
Output: [[1,4],[2,5],[3,6]]
14+
15+
16+
Note:
17+
(*) 1 <= A.length <= 1000
18+
(*) 1 <= A[0].length <= 1000
19+
"""
20+
from typing import List
21+
22+
23+
class Solution:
24+
def transpose(self, A: List[List[int]]) -> List[List[int]]:
25+
return [list(a) for a in zip(*A)]
26+
27+
28+
if __name__ == '__main__':
29+
from run_tests import run_tests
30+
31+
correct_answers = [
32+
[[[1,2,3],[4,5,6],[7,8,9]], [[1,4,7],[2,5,8],[3,6,9]]],
33+
[[[1,2,3],[4,5,6]], [[1,4],[2,5],[3,6]]]
34+
]
35+
print('Running tests for transpose method')
36+
run_tests(Solution().transpose, correct_answers)

0 commit comments

Comments
 (0)