ts
/**
Do not return anything, modify matrix in-place instead.
*/
function setZeroes(matrix: number[][]): void {
const len1 = matrix.length
const len2 = matrix[0].length
let set1 = new Set([])
let set2 = new Set([])
for (let i1 = 0; i1 < len1; i1++) {
for (let i2 = 0; i2 < len2; i2++) {
if (matrix[i1][i2] === 0) {
set1.add(i1)
set2.add(i2)
}
}
}
for (let x1 of set1.values()) {
for (let i2 = 0; i2 < len2; i2++) {
matrix[x1][i2] = 0
}
}
for (let x2 of set2.keys()) {
for (let i1 = 0; i1 < len1; i1++) {
matrix[i1][x2] = 0
}
}
};
py
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
len1 = len(matrix)
len2 = len(matrix[0])
arr1 = []
arr2 = []
for i1 in range(len1):
for i2 in range(len2):
if matrix[i1][i2] == 0:
arr1.append(i1)
arr2.append(i2)
for x1 in arr1:
for i2 in range(len2):
matrix[x1][i2] = 0
for x2 in arr2:
for i1 in range(len1):
matrix[i1][x2] = 0