> For the complete documentation index, see [llms.txt](https://cai-sen-se.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cai-sen-se.gitbook.io/leetcode/1-200/73.-ju-zhen-zhi-ling.md).

# 73. 矩阵置零

<https://leetcode-cn.com/problems/set-matrix-zeroes/>

## 解法一：

两次遍历，第一次获取信息，将要置零的行信息写在第一个(i, 0)，列信息写在第一个（0，j），即将他们置0。

注意左上角元素是交叉点，可能会造成干扰，因此第一列，即（i，0）只用于存储行信息，另用一个变量col0表示第0列的列信息。&#x20;

第二次遍历，看（i，j）同行或同列第一个是否为0，是则将（i，j）置0

时间O(n^2),空间O(1)

```python
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])
        col0 = 1  #第0列标志位
        for i in range(m):
            if matrix[i][0] == 0:
                col0 = 0
            for j in range(1, n):
                if matrix[i][j] == 0:
                    matrix[i][0] = matrix[0][j] = 0

        for i in range(m-1, -1, -1):
            for j in range(n-1, 0, -1):
                if matrix[i][0] == 0 or matrix[0][j] == 0:
                    matrix[i][j] = 0
            if not col0:  #特殊处理第0列
                matrix[i][0] = 0
```
