题解 | 清除行列

清除行列

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;
    }
};

全部评论

相关推荐

10-14 12:20
门头沟学院 Java
迷茫的大四🐶:摊牌了,我是25届的,你们也不招我
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务