【LeetCode热题100】【矩阵】矩阵置零

2024-04-16 08:15:49 浏览数 (2)

题目链接:73. 矩阵置零 - 力扣(LeetCode)

用两个数组标记要变成0的行和列索引

代码语言:javascript复制
class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        vector<bool> row(matrix.size(), false), column(matrix[0].size(), false);
        for (int i = 0; i < matrix.size();   i)
            for (int j = 0; j < matrix[0].size();   j)
                if (matrix[i][j] == 0)
                    row[i] = column[j] = true;
        for (int i = 0; i < matrix.size();   i)
            for (int j = 0; j < matrix[0].size();   j)
                if (row[i] || column[j])
                    matrix[i][j] = 0;
    }
};

0 人点赞