LeetCode—73

Approach

# 記錄哪一列跟哪一行出現過0,接著把那一行跟那一列的數字都改成0
Record which rows and columns contain a zero.
Set all the numbers in those rows and columns to zero.

Time Complexity

O(R * C)

Space Complexity

O(R + C)

Code

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int r = matrix.size(), c = matrix[0].size();
        vector<int> x0, y0;

        for (int i = 0 ; i < r ; i++) {
            for (int j = 0 ; j < c ; j++) {
                if (matrix[i][j] == 0) {
                    x0.push_back(j);
                    y0.push_back(i);
                }
            }
        }

        for (int i = 0 ; i < x0.size() ; i++) {
            for (int j = 0 ; j < c ; j++) {
                matrix[y0[i]][j] = 0;
            }

            for (int j = 0 ; j < r ; j++) {
                matrix[j][x0[i]] = 0;
            }
        }
    }
};