File tree Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Expand file tree Collapse file tree 2 files changed +52
-1
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
+ var setZeroes = function ( matrix ) {
6
+ const m = matrix . length ;
7
+ const n = matrix [ 0 ] . length ;
8
+ const flag = matrix [ 0 ] [ 0 ] === 0 ;
9
+ let xFlag = false ;
10
+ let yFlag = false ;
11
+ for ( let i = 0 ; i < m ; i ++ ) {
12
+ for ( let k = 0 ; k < n ; k ++ ) {
13
+ const current = matrix [ i ] [ k ] ;
14
+ if ( current === 0 ) {
15
+ if ( i === 0 ) {
16
+ xFlag = true ;
17
+ }
18
+ if ( k === 0 ) {
19
+ yFlag = true ;
20
+ }
21
+ if ( i !== 0 && k !== 0 ) {
22
+ matrix [ i ] [ 0 ] = 0 ;
23
+ matrix [ 0 ] [ k ] = 0 ;
24
+ }
25
+ }
26
+ }
27
+ }
28
+
29
+ for ( let i = m - 1 ; i > 0 ; i -- ) {
30
+ for ( let k = n - 1 ; k > 0 ; k -- ) {
31
+ if ( matrix [ 0 ] [ k ] === 0 || matrix [ i ] [ 0 ] === 0 ) {
32
+ matrix [ i ] [ k ] = 0 ;
33
+ }
34
+ }
35
+ }
36
+
37
+ if ( flag || xFlag ) {
38
+ for ( let i = 0 ; i < n ; i ++ ) {
39
+ matrix [ 0 ] [ i ] = 0 ;
40
+ }
41
+ }
42
+
43
+ if ( flag || yFlag ) {
44
+ for ( let i = 0 ; i < m ; i ++ ) {
45
+ matrix [ i ] [ 0 ] = 0 ;
46
+ }
47
+ }
48
+ } ;
Original file line number Diff line number Diff line change 1
- Record LeetCode.
1
+ Record LeetCode.
2
+
3
+ [ 73. 矩阵置零] ( ./73-set-matrix-zeroes.js )
4
+ [ 26. 删除有序数组中的重复项] ( ./26-remove-duplicates-from-sorted-array.js )
You can’t perform that action at this time.
0 commit comments