73. Set Matrix Zeroes

2019-05-31  本文已影响0人  jecyhw

题目链接

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;
                }
            }
        }
    }
};
上一篇下一篇

猜你喜欢

热点阅读