Skip to main content

Set Matrix Zeroes

Problem

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.

You must do it in place.

Solution Approach

Expected Time complexity: O(n3)O(n^3)

Click - to see solution code
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix){
vector<vector<int>> a = matrix;
int n = matrix.size();
int m = matrix[0].size();

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
for (int k = 0; k < n; k++) a[k][j] = 0;
for (int k = 0; k < m; k++) a[i][k] = 0;
}
}
}
matrix = a;
}
};