# 矩阵置零

<p>给定一个 <code><em>m</em> x <em>n</em></code> 的矩阵，如果一个元素为 <strong>0 </strong>，则将其所在行和列的所有元素都设为 <strong>0</strong> 。请使用 <strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong> 算法<strong>。</strong></p><p><strong>进阶：</strong></p><ul>	<li>一个直观的解决方案是使用  <code>O(<em>m</em><em>n</em>)</code> 的额外空间，但这并不是一个好的解决方案。</li>	<li>一个简单的改进方案是使用 <code>O(<em>m</em> + <em>n</em>)</code> 的额外空间，但这仍然不是最好的解决方案。</li>	<li>你能想出一个仅使用常量空间的解决方案吗？</li></ul><p><strong>示例 1：</strong></p><img alt="" src="https://img-blog.csdnimg.cn/img_convert/a9dfa884d1a855d6630087aa890a20f8.png#pic_center" /><pre><strong>输入：</strong>matrix = [[1,1,1],[1,0,1],[1,1,1]]<strong><br />输出：</strong>[[1,0,1],[0,0,0],[1,0,1]]</pre><p><strong>示例 2：</strong></p><img alt="" src="https://img-blog.csdnimg.cn/img_convert/816bcf700b7a5b0c1b118f9aaac5bb58.png#pic_center" /><pre><strong>输入：</strong>matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]<strong><br />输出：</strong>[[0,0,0,0],[0,4,5,0],[0,3,1,0]]</pre><p><strong>提示：</strong></p><ul>	<li><code>m == matrix.length</code></li>	<li><code>n == matrix[0].length</code></li>	<li><code>1 <= m, n <= 200</code></li>	<li><code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code></li></ul>
<p>以下<span style="color:red">错误</span>的选项是？</p>

## aop

### before

```c
#include <bits/stdc++.h>
using namespace std;
```

### after

```c
int main()
{
    Solution sol;
    int a = 3, b = 3;

    vector<vector<int>> matrix = vector<vector<int>>(a, vector<int>(b)) = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
    sol.setZeroes(matrix);
    for (auto i : matrix)
    {
        for (auto j : i)
            cout << j << " ";
        cout << endl;
    }
    return 0;
}
```

## 答案

```c
class Solution
{
public:
    void setZeroes(vector<vector<int>> &matrix)
    {
        int row = matrix.size();
        int col = matrix[0].size();
        vector<int> pos;

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (matrix[i][j] == 0)
                {
                    pos.push_back(i);
                    pos.push_back(j);
                }
            }
        }

        for (int i = 1; i < pos.size(); i += 2)
        {
            int x = pos[i];
            int y = pos[i + 1];

            for (int j = 0; j < row; j++)
            {
                matrix[j][y] = 0;
            }
            for (int k = 0; k < col; k++)
            {
                matrix[x][k] = 0;
            }
        }
    }
};
```
## 选项


### A

```c
class Solution
{
public:
    void setZeroes(vector<vector<int>> &matrix)
    {
        if (matrix.size() <= 0)
            return;

        int row = -1, col = -1;

        int i = 0;

        for (i = 0; i < matrix.size(); i++)
        {
            for (int j = 0; j < matrix[0].size(); j++)
            {
                if (matrix[i][j] == 0)
                {
                    row = i;
                    col = j;
                    break;
                }
            }
            if (row != -1)
                break;
        }

        if (i == matrix.size())
            return;

        for (int k = i + 1; k < matrix.size(); k++)
        {
            for (int j = 0; j < matrix[0].size(); j++)
            {
                if (matrix[k][j] == 0)
                {
                    matrix[row][j] = 0;
                    matrix[k][col] = 0;
                }
            }
        }

        for (i = 0; i < matrix[0].size(); i++)
        {
            if (i == col)
                continue;
            if (matrix[row][i] == 0)
                for (int j = 0; j < matrix.size(); j++)
                    matrix[j][i] = 0;
        }

        for (i = 0; i < matrix.size(); i++)
        {
            if (i == row)
                continue;
            if (matrix[i][col] == 0)
                for (int j = 0; j < matrix[0].size(); j++)
                    matrix[i][j] = 0;
        }

        for (i = 0; i < matrix.size(); i++)
            matrix[i][col] = 0;
        for (i = 0; i < matrix[0].size(); i++)
            matrix[row][i] = 0;
    }
};
```

### B

```c
class Solution
{
public:
    void setZeroes(vector<vector<int>> &matrix)
    {
        bool row = false, col = false;
        int m = matrix.size(), n = matrix[0].size();
        for (int i = 0; i < m; ++i)
        {
            if (matrix[i][0] == 0)
                col = true;
        }
        for (int i = 0; i < n; ++i)
        {
            if (matrix[0][i] == 0)
                row = true;
        }
        for (int i = 1; i < m; ++i)
            for (int j = 1; j < n; ++j)
            {
                if (matrix[i][j] == 0)
                    matrix[i][0] = 0, matrix[0][j] = 0;
            }
        for (int i = 1; i < m; ++i)
            for (int j = 1; j < n; ++j)
            {
                if (matrix[i][0] == 0 || matrix[0][j] == 0)
                    matrix[i][j] = 0;
            }
        if (col)
        {
            for (int i = 0; i < m; ++i)
                matrix[i][0] = 0;
        }
        if (row)
        {
            for (int i = 0; i < n; ++i)
                matrix[0][i] = 0;
        }
    }
};
```

### C

```c
class Solution
{
public:
    void setZeroes(vector<vector<int>> &matrix)
    {
        int m = matrix.size(), n = matrix[0].size();
        vector<bool> row(m, false), column(n, false);
        int i, j;
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                if (matrix[i][j] == 0)
                {
                    row[i] = true;
                    column[j] = true;
                }
            }
        }
        for (i = 0; i < m; i++)
        {
            if (row[i])
            {
                for (j = 0; j < n; j++)
                    matrix[i][j] = 0;
            }
        }
        for (i = 0; i < n; i++)
        {
            if (column[i])
            {
                for (j = 0; j < m; j++)
                    matrix[j][i] = 0;
            }
        }
    }
};
```
