[leetcode刷题系列]Set Matrix Zeroes

我不知道这算不算inplace


class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(matrix.size() <= 0)
            return ;
        vector<int> row, col;
        row.resize(matrix.size());
        col.resize(matrix[0].size());
        fill(row.begin(), row.end(), 1);
        fill(col.begin(), col.end(), 1);
        for(int i = 0; i < matrix.size(); ++ i)
            for(int j = 0; j < matrix[i].size(); ++ j)
                if(matrix[i][j] == 0)
                    row[i] = col[j] = 0;
        for(int i = 0; i < matrix.size(); ++ i)
            for(int j = 0; j < matrix[i].size(); ++ j)
                if(row[i] == 0 || col[j] == 0)
                    matrix[i][j] = 0;
    }
};


你可能感兴趣的:([leetcode刷题系列]Set Matrix Zeroes)