LeetCode 筆記 - 867. Transpose Matrix

題目在此 867. Transpose Matrix

給定一個 2D matrix,請做出 Transpose Matrix

image 54

解題思維

Just do it.

程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
def transpose(self, matrix: List[List[int]]) -> List[List[int]]:


size_y = len(matrix)
size_x = len(matrix[0])

result = [[0 for _ in range(size_y)] for _ in range(size_x)]

for y in range(size_y):
for x in range(y, size_x):
result[x][y] = matrix[y][x]

return result

也許你也會想看看