Everyday has one random leetcode quiz. Today’s quiz is 73. Set Matrix Zeroes
Description:
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.
Get Jimmy’s stories in your inbox
Join Medium for free to get updates from this writer.
Example 1:
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]Example 2:
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]We use zeroRow and zeroColumn to record which row or column need to be set as 0 .
Check entire matrix , mark these 0 cell’s row and column as true .
Check entire matrix again, update cell by zeroRow and zeroColumn .
If matrix is n*m, the time complexity is O(n*m)
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function (matrix) {
const zeroRow = Array(matrix.length).fill(false)
const zeroColumn = Array(matrix[0].length).fill(false)
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[0].length; col++) {
if (matrix[row][col] === 0) {
zeroRow[row] = true
zeroColumn[col] = true
}
}
}
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[0].length; col++) {
if (zeroRow[row] || zeroColumn[col]) {
matrix[row][col] = 0
}
}
}
};





![Python DSA Coding Interview Series:11/29 [Linked Lists]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*RpzhG5-m6tzjX-E1XdLsWA.png)