Skip to content

Latest commit

 

History

History
25 lines (25 loc) · 1.03 KB

零矩阵.org

File metadata and controls

25 lines (25 loc) · 1.03 KB

题目

Screen-Pictures/%E9%A2%98%E7%9B%AE/2020-07-02_10-52-04_%E6%88%AA%E5%B1%8F2020-07-02%20%E4%B8%8A%E5%8D%8810.51.59.png

思路

遍历矩阵,找出0的行列索引,分别存进set()中,再遍历一遍矩阵,将行列在set中的元素置0

code

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        # 暴力法
        m, n = len(matrix), len(matrix[0])
        row, column = set(), set()
        for i in range(m):
            for j in range(n):
                if matrix[i][j] == 0:
                    row.add(i)
                    column.add(j)
        for i in range(m):
            for j in range(n):
                if i in row or j in column:
                    matrix[i][j] = 0