Skip to content

Commit 227adac

Browse files
committed
Added setMatrixZeroes solution
1 parent ad9655e commit 227adac

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

set-matrix-zeroes/nhistory.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var setZeroes = function (matrix) {
2+
let rows = new Set();
3+
let cols = new Set();
4+
5+
for (let row = 0; row < matrix.length; row++) {
6+
for (let col = 0; col < matrix[0].length; col++) {
7+
if (matrix[row][col] === 0) {
8+
rows.add(row);
9+
cols.add(col);
10+
}
11+
}
12+
}
13+
14+
for (let row = 0; row < matrix.length; row++) {
15+
for (let col = 0; col < matrix[0].length; col++) {
16+
if (rows.has(row) || cols.has(col)) {
17+
matrix[row][col] = 0;
18+
}
19+
}
20+
}
21+
};
22+
23+
// TC: O(m*n)
24+
// SC: O(m*n)

0 commit comments

Comments
 (0)