File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-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
+ var setZeroes = function ( matrix ) {
6
+ const m = matrix . length ;
7
+ const n = matrix [ 0 ] . length ;
8
+
9
+ let firstRowHasZero = false ;
10
+ let firstColHasZero = false ;
11
+
12
+ // 첫 행/열에 0이 있는지 확인
13
+ for ( let i = 0 ; i < m ; i ++ ) {
14
+ if ( matrix [ i ] [ 0 ] === 0 ) firstColHasZero = true ;
15
+ }
16
+ for ( let j = 0 ; j < n ; j ++ ) {
17
+ if ( matrix [ 0 ] [ j ] === 0 ) firstRowHasZero = true ;
18
+ }
19
+
20
+ // 마커로 0 표시
21
+ for ( let i = 1 ; i < m ; i ++ ) {
22
+ for ( let j = 1 ; j < n ; j ++ ) {
23
+ if ( matrix [ i ] [ j ] === 0 ) {
24
+ matrix [ i ] [ 0 ] = 0 ; // 해당 행 마킹
25
+ matrix [ 0 ] [ j ] = 0 ; // 해당 열 마킹
26
+ }
27
+ }
28
+ }
29
+
30
+ // 마커 기반으로 행/열 0 처리
31
+ for ( let i = 1 ; i < m ; i ++ ) {
32
+ if ( matrix [ i ] [ 0 ] === 0 ) {
33
+ for ( let j = 1 ; j < n ; j ++ ) {
34
+ matrix [ i ] [ j ] = 0 ;
35
+ }
36
+ }
37
+ }
38
+ for ( let j = 1 ; j < n ; j ++ ) {
39
+ if ( matrix [ 0 ] [ j ] === 0 ) {
40
+ for ( let i = 1 ; i < m ; i ++ ) {
41
+ matrix [ i ] [ j ] = 0 ;
42
+ }
43
+ }
44
+ }
45
+
46
+ // Step 4: 첫 행과 첫 열 처리
47
+ if ( firstRowHasZero ) {
48
+ for ( let j = 0 ; j < n ; j ++ ) {
49
+ matrix [ 0 ] [ j ] = 0 ;
50
+ }
51
+ }
52
+ if ( firstColHasZero ) {
53
+ for ( let i = 0 ; i < m ; i ++ ) {
54
+ matrix [ i ] [ 0 ] = 0 ;
55
+ }
56
+ }
57
+ } ;
You can’t perform that action at this time.
0 commit comments