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) {
int rows = matrix.size(), cols = 0;
if(rows == 0) return ;
cols = matrix[0].size();
set<int> rowNum;
set<int> colNum;
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
if(matrix[i][j] == 0) {
rowNum.insert(i);
colNum.insert(j);
}
}
}
set<int>::iterator itr;
for(itr=rowNum.begin(); itr!=rowNum.end(); itr++) {
int temp = *itr;
for(int i = 0; i < cols; i++)
matrix[temp][i] = 0;
}
for(itr=colNum.begin(); itr!=colNum.end(); itr++) {
int temp = *itr;
for(int i=0; i < rows; i++) {
matrix[i][temp] = 0;
}
}
}
};
本文介绍了一个在给定的 m x n 矩阵中,当元素为零时,如何将其所在整行和整列设置为零的方法,并提供了相应的代码实现。
427

被折叠的 条评论
为什么被折叠?



