File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } matrix
3
+ * @return {void } Do not return anything, modify matrix in-place instead.
4
+ */
5
+ const setZeroes = function ( matrix ) {
6
+ const rows = [ ]
7
+ const cols = [ ]
8
+ const rowNum = matrix . length
9
+ const colNum = matrix [ 0 ] . length
10
+ for ( let i = 0 ; i < rowNum ; i ++ ) {
11
+ for ( let j = 0 ; j < colNum ; j ++ ) {
12
+ if ( matrix [ i ] [ j ] === 0 ) {
13
+ rows . push ( i )
14
+ cols . push ( j )
15
+ }
16
+ }
17
+ }
18
+ const mrows = rows . filter ( ( el , idx , arr ) => arr . indexOf ( el ) === idx )
19
+ const mcols = cols . filter ( ( el , idx , arr ) => arr . indexOf ( el ) === idx )
20
+ for ( let i = 0 ; i < mrows . length ; i ++ ) {
21
+ matrix [ mrows [ i ] ] = new Array ( colNum ) . fill ( 0 )
22
+ }
23
+ for ( let j = 0 ; j < mcols . length ; j ++ ) {
24
+ for ( let k = 0 ; k < rowNum ; k ++ ) {
25
+ matrix [ k ] [ mcols [ j ] ] = 0
26
+ }
27
+ }
28
+ return matrix
29
+ } ;
You can’t perform that action at this time.
0 commit comments