题解 | 清除行列
清除行列
https://www.nowcoder.com/practice/c95aac1506574dfc8ad44c3939c6739d
#include <cstddef>
class Clearer {
public:
vector<vector<int> > clearZero(vector<vector<int> > mat, int n) {
// write code here
if (n <= 1)
return mat;
std::set<int> rows, cols;
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
if (mat[i][j] == 0) {
rows.insert(i);
cols.insert(j);
}
}
}
for (auto r : rows) {
for (size_t j = 0; j < n; j++) {
mat[r][j] = 0;
}
}
for (auto c : cols) {
for (size_t j = 0; j < n; j++) {
mat[j][c] = 0;
}
}
return mat;
}
};

360集团公司氛围 358人发布