Skip to content

Commit f73a017

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 18.4 MB (92.30%)
1 parent ca2e67f commit f73a017

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>Given a 2D integer array <code>matrix</code>, return <em>the <strong>transpose</strong> of</em> <code>matrix</code>.</p>
2+
3+
<p>The <strong>transpose</strong> of a matrix is the matrix flipped over its main diagonal, switching the matrix&#39;s row and column indices.</p>
4+
5+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/02/10/hint_transpose.png" style="width: 600px; height: 197px;" /></p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> matrix = [[1,2,3],[4,5,6],[7,8,9]]
12+
<strong>Output:</strong> [[1,4,7],[2,5,8],[3,6,9]]
13+
</pre>
14+
15+
<p><strong class="example">Example 2:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> matrix = [[1,2,3],[4,5,6]]
19+
<strong>Output:</strong> [[1,4],[2,5],[3,6]]
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>m == matrix.length</code></li>
27+
<li><code>n == matrix[i].length</code></li>
28+
<li><code>1 &lt;= m, n &lt;= 1000</code></li>
29+
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
30+
<li><code>-10<sup>9</sup> &lt;= matrix[i][j] &lt;= 10<sup>9</sup></code></li>
31+
</ul>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
3+
# return list(zip(*matrix))
4+
m, n = len(matrix), len(matrix[0])
5+
result = [[0] * m for _ in range(n)]
6+
for i in range(m):
7+
for j in range(n):
8+
result[j][i] = matrix[i][j]
9+
return result

0 commit comments

Comments
 (0)