반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 867 | Transpose Matrix | Python 본문
728x90
반응형
문제 링크: https://leetcode.com/problems/transpose-matrix/
문제
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.
조건
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 1000
- 1 <= m * n <= $10^5$
- -109 <= matrix[i][j] <= $10^9$
내 풀이
주어진 matrix의 H, W를 받아온 후, 이를 H를 너비, W를 길이로 갖는 새로운 matrix(newM)을 만든다.
matrix를 순서대로 보며, matrix[h][w]를 newM[w][h]에 매핑해주면 된다.
코드
class Solution:
def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
H, W = len(matrix), len(matrix[0])
newM = [[0 for _ in range(H)] for _ in range(W)]
for h in range(H):
for w in range(W):
newM[w][h] = matrix[h][w]
return newM
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 1332 | Remove Palindromic Subsequences | Python (0) | 2022.06.08 |
---|---|
[LeetCode] 88 | Merge Sorted Array | Python (0) | 2022.06.08 |
[LeetCode] 29 | Divide Two Integers | Python (0) | 2022.05.30 |
[LeetCode] 191 | Number of 1 Bits | Python (0) | 2022.05.26 |
[LeetCode] 354 | Russian Doll Envelopes | Python (0) | 2022.05.25 |
Comments