美文网首页
73. Set Matrix Zeroes

73. Set Matrix Zeroes

作者: jecyhw | 来源:发表于2019-05-31 06:27 被阅读0次

题目链接

https://leetcode.com/problems/set-matrix-zeroes/

解题思路

直接看代码

代码

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int row = matrix.size();
        if (row == 0) {
            return;
        }
        int col = matrix[0].size();
        vector<bool> rows(row, false), cols(col, false);
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                if (matrix[i][j] == 0) {
                    rows[i] = true;
                    cols[j] = true;
                }
            }
        }
        for (int i = 0; i < row; ++i) {
            if (rows[i]) {
                for (int j = 0; j < col; ++j) {
                    matrix[i][j] = 0;
                }
            }
        }
        for (int i = 0; i < col; ++i) {
            if (cols[i]) {
                for (int j = 0; j < row; ++j) {
                    matrix[j][i] = 0;
                }
            }
        }
    }
};

相关文章

网友评论

      本文标题:73. Set Matrix Zeroes

      本文链接:https://www.haomeiwen.com/subject/xdeitctx.html