leetcode专题训练 73. Set Matrix Zeroes

遍历整个矩阵,如果matrix[i][j]==0matrix[i][j] == 0matrix[i][j]==0,那么就记下将行号和列号记下。再遍历一遍矩阵,如果当前行数或列数刚刚被记过,那么就将该数置为0。

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """

        if not matrix:
            return matrix
        
        l1 = len(matrix)
        l2 = len(matrix[0])
        row = [0 for _ in range(l1)]
        col = [0 for _ in range(l2)]

        for i in range(l1):
            for j in range(l2):
                if matrix[i][j] == 0:
                    row[i] = 1
                    col[j] = 1

        for i in range(l1):
            for j in range(l2):
                if row[i] == 1 or col[j] == 1:
                    matrix[i][j] = 0
        
        return
leetcode专题训练 73. Set Matrix Zeroesleetcode专题训练 73. Set Matrix Zeroes Emma1997 发布了213 篇原创文章 · 获赞 26 · 访问量 8万+ 私信 关注
上一篇:Leetcode 283:Move Zeroes


下一篇:474. Ones and Zeroes