File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ const setZeroes = ( matrix ) => {
2+ const ROWS = matrix . length ;
3+ const COLS = matrix [ 0 ] . length ;
4+ let rowZero = false ;
5+
6+ // 1. 어떤 행과 열이 0이 되어야 하는지 기록
7+ for ( let r = 0 ; r < ROWS ; r ++ ) {
8+ for ( let c = 0 ; c < COLS ; c ++ ) {
9+ if ( matrix [ r ] [ c ] === 0 ) {
10+ matrix [ 0 ] [ c ] = 0 ;
11+ if ( r > 0 ) {
12+ matrix [ r ] [ 0 ] = 0 ;
13+ } else {
14+ rowZero = true ;
15+ }
16+ }
17+ }
18+ }
19+
20+ // 2. 첫 번째 행과 첫 번째 열을 제외한 나머지 처리
21+ for ( let r = 1 ; r < ROWS ; r ++ ) {
22+ for ( let c = 1 ; c < COLS ; c ++ ) {
23+ if ( matrix [ 0 ] [ c ] === 0 || matrix [ r ] [ 0 ] === 0 ) {
24+ matrix [ r ] [ c ] = 0 ;
25+ }
26+ }
27+ }
28+
29+ // 3. 첫 번째 열 처리
30+ if ( matrix [ 0 ] [ 0 ] === 0 ) {
31+ for ( let r = 0 ; r < ROWS ; r ++ ) {
32+ matrix [ r ] [ 0 ] = 0 ;
33+ }
34+ }
35+
36+ // 4. 첫 번째 행 처리
37+ if ( rowZero ) {
38+ for ( let c = 0 ; c < COLS ; c ++ ) {
39+ matrix [ 0 ] [ c ] = 0 ;
40+ }
41+ }
42+ } ;
You can’t perform that action at this time.
0 commit comments