forked from codesONLY/JavaScriptONLY
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetMatrixZeros.js
More file actions
33 lines (30 loc) · 773 Bytes
/
setMatrixZeros.js
File metadata and controls
33 lines (30 loc) · 773 Bytes
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
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function(matrix) {
let col0 = 1;
let rows = matrix.length;
let cols = matrix[0].length;
for(let i=0; i < rows; i++){
if(matrix[i][0] === 0){
col0 = 0;
}
for(let j=1; j < cols; j++){
if(matrix[i][j] === 0){
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for(let i=rows-1; i >= 0; i--){
for(let j=cols-1; j>=1; j--){
if(matrix[i][0] === 0 || matrix[0][j] == 0){
matrix[i][j] = 0;
}
}
if(col0 === 0){
matrix[i][0] = 0;
}
}
};