[LEETCODE] 73: Set Matric Zeros(Python)

박나현·2024년 2월 29일

Set Matrix Zeroes - LeetCode

문제 설명

정수로 이루어진 행렬이 주어질 때, 원소가 0인 행과 열을 전부 0으로 바꿔주자.

나의 풀이

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        colset=set()
        rowset=set()
        m=len(matrix)
        n=len(matrix[0])
        
        for i in range(m):
            for j in range(n):
                if matrix[i][j]==0:
                    colset.add(i)
                    rowset.add(j)
        
        for i in colset:
            matrix[i]=[0]*n
            
        for i in range(m):
            for j in rowset:
                matrix[i][j]=0

0이 발견된 행, 열 정보를 집합에 넣어 중복을 제거하고, 해당 집합에 담긴 행/열 정보를 0으로 업데이트한다.

시간복잡도

0을 찾는 데에 O(n), 행/열 정보를 업데이트하는 데에 O(n)이 걸린다.

profile
의견을 가지고 학습하기, 질문하기, 궁금했던 주제에 대해 학습하는 것을 미루지 않기

0개의 댓글