File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ // m: height of matrix, n: width of matrix
2
+ // Time complexity: O(m*n)
3
+ // Space complexity: O(m+n)
4
+
5
+ /**
6
+ * @param {number[][] } matrix
7
+ * @return {void } Do not return anything, modify matrix in-place instead.
8
+ */
9
+ var setZeroes = function ( matrix ) {
10
+ const h = matrix . length ;
11
+ const w = matrix [ 0 ] . length ;
12
+
13
+ const setY = new Set ( ) ;
14
+ const setX = new Set ( ) ;
15
+
16
+ for ( let i = 0 ; i < h ; i ++ ) {
17
+ for ( let j = 0 ; j < w ; j ++ ) {
18
+ if ( matrix [ i ] [ j ] === 0 ) {
19
+ setY . add ( i ) ;
20
+ setX . add ( j ) ;
21
+ }
22
+ }
23
+ }
24
+
25
+ for ( const y of setY ) {
26
+ for ( let x = 0 ; x < w ; x ++ ) {
27
+ matrix [ y ] [ x ] = 0 ;
28
+ }
29
+ }
30
+
31
+ for ( const x of setX ) {
32
+ for ( let y = 0 ; y < h ; y ++ ) {
33
+ matrix [ y ] [ x ] = 0 ;
34
+ }
35
+ }
36
+ } ;
You can’t perform that action at this time.
0 commit comments