File tree 2 files changed +38
-0
lines changed 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 시간 복잡도: O(m * n)
2
+ // 공간 복잡도: O(m * n)
3
+
1
4
/**
2
5
* @param {character[][] } grid
3
6
* @return {number }
Original file line number Diff line number Diff line change
1
+ // 시간 복잡도: O(m * n)
2
+ // 공간 복잡도: O(m + n)
3
+
4
+ /**
5
+ * @param {number[][] } matrix
6
+ * @return {void } Do not return anything, modify matrix in-place instead.
7
+ */
8
+ var setZeroes = function ( matrix ) {
9
+ if ( ! matrix || matrix . length === 0 ) return [ ] ;
10
+
11
+ const rows = matrix . length ;
12
+ const cols = matrix [ 0 ] . length ;
13
+
14
+ const zeroRows = new Array ( rows ) . fill ( false ) ;
15
+ const zeroCols = new Array ( cols ) . fill ( false ) ;
16
+
17
+ for ( let row = 0 ; row < rows ; row ++ ) {
18
+ for ( let col = 0 ; col < cols ; col ++ ) {
19
+ if ( matrix [ row ] [ col ] === 0 ) {
20
+ zeroRows [ row ] = true ;
21
+ zeroCols [ col ] = true ;
22
+ }
23
+ }
24
+ }
25
+
26
+ for ( let row = 0 ; row < rows ; row ++ ) {
27
+ for ( let col = 0 ; col < cols ; col ++ ) {
28
+ if ( zeroRows [ row ] || zeroCols [ col ] ) {
29
+ matrix [ row ] [ col ] = 0 ;
30
+ }
31
+ }
32
+ }
33
+
34
+ return matrix ;
35
+ } ;
You can’t perform that action at this time.
0 commit comments