๋ฌธ์
Given a 2D integer array matrix, return the transpose of matrix.
The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]
Example 2:
Input: matrix = [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]]
ํด์ค ์ฝ๋
class Solution(object):
def transpose(self, matrix):
arr = [[0 for i in range(len(matrix))] for t in range(len(matrix[0]))]
for i in range(len(matrix)):
for t in range(len(matrix[0])):
arr[t][i] = matrix[i][t]
return arr
๋ถ์ > ์ด ๋ฌธ์ ์์ ๊ฐ๊ณผํ๋ ๋ถ๋ถ์ ์ด์ค ๋ฐฐ์ด์ ์ ์ธํ ๋ ๋จ์ํ๊ฒ ๊ณฑํ๊ธฐ๋ฅผ ์ด์ฉํด์ ๋ฐฐ์ด์ ์ ์ธํ๋ค๋ ์ ์ด๋ค. ๊ทธ๋์ ์์ ์๋ ๋ฐฐ์ด์ด ๋๊ฐ์ด ์์ง์ด๊ฒ ๋์ด ์ํ๋ ๋ต์ด ๋์ฌ ์ ์์๋ค. ๊ทธ๋ฌ๋ ์ด์ค ๋ฐฐ์ด์ ์ ์ธํ ๋๋ for๋ฌธ์ ์ฌ์ฉํ์ฌ์ผ ํ๋ค.
'Algorithm > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode] Print Words Vertically (#1324) (0) | 2022.04.20 |
---|---|
[Leetcode] Reshape the Matrix (#566) (0) | 2022.04.20 |
[Leetcode] Complex Number Multiplication (#537) (0) | 2022.04.20 |
[Leetcode] Game of Life (#289) (0) | 2022.04.19 |
[Leetcode] Last Stone Weight (#1046) (0) | 2022.04.08 |