Set matrix zeroes
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.
--
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
function zeroStripping(matrix) {
const rows = matrix.length;
const cols = matrix[0].length;
// Arrays to mark rows and columns that contain zeros
const rowsWithZeros = new Array(rows).fill(false);
const colsWithZeros = new Array(cols).fill(false);
// Find all zeros and mark their corresponding rows and columns
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (matrix[i][j] === 0) {
rowsWithZeros[i] = true;
colsWithZeros[j] = true;
}
}
}
// Set the marked rows and columns to zero
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (rowsWithZeros[i] || colsWithZeros[j]) {
matrix[i][j] = 0;
}
}
}
return matrix;
}