-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixreplacezerowithrc.js
49 lines (43 loc) · 1 KB
/
matrixreplacezerowithrc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function setZeroes(matrix) {
let rows = matrix.length;
let cols = matrix[0].length;
let zeroRows = new Set();
let zeroCols = new Set();
// Step 1: Identify all rows and columns that need to be zeroed
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (matrix[i][j] === 0) {
zeroRows.add(i);
zeroCols.add(j);
}
}
}
// Step 2: Set the identified rows to zero
for (let row of zeroRows) {
for (let j = 0; j < cols; j++) {
matrix[row][j] = 0;
}
}
// Step 3: Set the identified columns to zero
for (let col of zeroCols) {
for (let i = 0; i < rows; i++) {
matrix[i][col] = 0;
}
}
}
// Example usage:
let matrix = [
[1, 2, 3, 4],
[5, 0, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 0]
];
setZeroes(matrix);
console.log(matrix);
// Output:
// [
// [1, 0, 3, 0],
// [0, 0, 0, 0],
// [9, 0, 11, 0],
// [0, 0, 0, 0]
// ]