Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

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

	    for (set<int>::iterator it = row.begin(); it != row.end(); it++)
	    {
		    int size = matrix[*it].size();
		    for (int j = 0; j < size; j++)
		    {
			    matrix[*it][j] = 0;
		    }
	    }

	    for (set<int>::iterator it = column.begin(); it != column.end(); it++)
	    {
		    for (int j = 0; j < m; j++)
		    {
		    	matrix[j][*it] = 0;
		    }
	    }
    }
};


你可能感兴趣的:(Set Matrix Zeroes)