[LeetCode/Python] Set Matrix Zeroes

김미영·2024년 3월 13일
0

LeetCode

목록 보기
5/11

📌 문제

https://leetcode.com/problems/set-matrix-zeroes/description/


📝 해결

시간 복잡도 : O(mn)

배열 전체를 탐색한 후 0의 위치를 저장하고, 그 위치에 행과 열에 0을 넣는 방법
이게 과연 최선일까...?

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        rows, cols = len(matrix), len(matrix[0])
        position = []
        
        for i in range(rows):
            for j in range(cols):
                if matrix[i][j] == 0:
                    position.append((i, j))
        
        for i, j in position:
            for k in range(cols):
                matrix[i][k] = 0
            for k in range(rows):
                matrix[k][j] = 0

0개의 댓글