题意
将矩阵中为0点它所在行和列都设置为0
题解
用两个集合保存需要设置的行和列。时间复杂度为O(m * n)
代码
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
set<int> rows, cols;
int m = matrix.size(), n = matrix[0].size();
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(matrix[i][j] == 0)
rows.insert(i), cols.insert(j);
}
}
for(set<int>::iterator i = rows.begin(); i != rows.end(); i++)
{
for(int j = 0; j < n; j++)
{
matrix[*i][j] = 0;
}
}
for(set<int>::iterator i = cols.begin(); i != cols.end(); i++)
{
for(int j = 0; j < m; j++)
{
matrix[j][*i] = 0;
}
}
}
};