File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 33
33
58|[ Length of Last Word] ( ./0058-length-of-last-word.js ) |Easy|
34
34
66|[ Plus One] ( ./0066-plus-one.js ) |Easy|
35
35
67|[ Add Binary] ( ./0067-add-binary.js ) |Easy|
36
+ 73|[ Set Matrix Zeroes] ( ./0073-set-matrix-zeroes.js ) |Medium|
36
37
88|[ Merge Sorted Array] ( ./0088-merge-sorted-array.js ) |Easy|
37
38
151|[ Reverse Words in a String] ( ./0151-reverse-words-in-a-string.js ) |Medium|
38
39
152|[ Maximum Product Subarray] ( ./0152-maximum-product-subarray.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 73. Set Matrix Zeroes
3
+ * https://leetcode.com/problems/set-matrix-zeroes/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an `m x n` integer `matrix` matrix, if an element is `0`, set its entire
7
+ * row and column to `0`'s, and return the matrix.
8
+ *
9
+ * You must do it in place.
10
+ */
11
+
12
+ /**
13
+ * @param {number[][] } matrix
14
+ * @return {void } Do not return anything, modify matrix in-place instead.
15
+ */
16
+ var setZeroes = function ( matrix ) {
17
+ const columns = new Set ( ) ;
18
+ const rows = new Set ( ) ;
19
+
20
+ matrix . forEach ( ( row , i ) => {
21
+ row . forEach ( ( value , j ) => {
22
+ if ( value === 0 ) {
23
+ columns . add ( i ) ;
24
+ rows . add ( j ) ;
25
+ }
26
+ } ) ;
27
+ } ) ;
28
+
29
+ [ ...columns ] . forEach ( i => matrix [ i ] . forEach ( ( _ , j ) => matrix [ i ] [ j ] = 0 ) ) ;
30
+ matrix . forEach ( ( _ , i ) => [ ...rows ] . forEach ( j => matrix [ i ] [ j ] = 0 ) ) ;
31
+
32
+ return matrix ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments