Skip to main content
A cup of beer
  1. ACM Hub/

🎟|LeetCode每日一题|零矩阵

·101 words·1 min
Table of Contents

难度:Mid

题目 #

编写一种算法,若M × N矩阵中某个元素为0,则将其所在的行与列清零。

示例 1:

输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输出: [ [1,0,1], [0,0,0], [1,0,1] ]

To Question

代码实现 #

#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> Point;

class Solution {
    set<Point> zeroPoints;
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int n = matrix.size(), m = matrix[0].size();
        for(int i = 0;i < n;i ++)
            for(int j = 0;j < m;j ++){
                if(matrix[i][j] == 0)
                    zeroPoints.insert(make_pair(i,j));
            }
        for(Point p : zeroPoints){
            int row = p.first, col = p.second;
            for(int i = 0;i < m;i ++)
                matrix[row][i] = 0;
            for(int i = 0;i < n;i ++)
                matrix[i][col] = 0;
        }
    }
};