Skip to content
文档章节

[73] 矩阵置零.md

题目参考: [73. 矩阵置零](https://leetcode.cn/problems/set-matrix-zeroes/submissions/)

题解

记录 0 出现的行和列,然后将对应的行和列置为0

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

      row = []
      col = []

      for i in range(m):
        # if i in row:
        #   continue
        for j in range(n):
          # if j in col:
          #   continue

          if matrix[i][j] == 0:
            if i not in row:
              row.append(i)
            col.append(j)

      for i in row:
        for j in range(n):
          matrix[i][j] = 0

      for j in col:
        for i in range(m):
          matrix[i][j] = 0