File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * TC: O(ROW * COLUMN)
3+ * SC: O(1)
4+ */
5+
6+ /**
7+ * @param {number[][] } matrix
8+ * @return {void } Do not return anything, modify matrix in-place instead.
9+ */
10+ var setZeroes = function ( matrix ) {
11+ const ROW = matrix . length ;
12+ const COLUMN = matrix [ 0 ] . length ;
13+
14+ // 1. 0์ธ ์์์ ๊ฐ๋ก, ์ธ๋ก๋ฅผ ํน์ ๋ฌธ์๋ก ๋ณ๊ฒฝ
15+ for ( let row = 0 ; row < ROW ; row ++ ) {
16+ for ( let column = 0 ; column < COLUMN ; column ++ ) {
17+ if ( matrix [ row ] [ column ] === 0 ) {
18+ changeToChar ( row , column , "#" ) ;
19+ }
20+ }
21+ }
22+
23+ // 2. ํน์ ๋ฌธ์๋ฅผ ๋ชจ๋ 0์ผ๋ก ๋ณ๊ฒฝ
24+ for ( let row = 0 ; row < ROW ; row ++ ) {
25+ for ( let column = 0 ; column < COLUMN ; column ++ ) {
26+ if ( matrix [ row ] [ column ] === "#" ) {
27+ matrix [ row ] [ column ] = 0 ;
28+ }
29+ }
30+ }
31+
32+ // 3. ํน์ ์ขํ์ ๊ฐ๋ก, ์ธ๋ก๋ฅผ char๋ฌธ์๋ก ๋ณ๊ฒฝ (๋์ 0์ธ ์์๋ ๋ณ๊ฒฝํ์ง ์์)
33+ function changeToChar ( row , column , char ) {
34+ for ( let r = 0 ; r < ROW ; r ++ ) {
35+ if ( matrix [ r ] [ column ] !== 0 ) {
36+ matrix [ r ] [ column ] = char ;
37+ }
38+ }
39+ for ( let c = 0 ; c < COLUMN ; c ++ ) {
40+ if ( matrix [ row ] [ c ] !== 0 ) {
41+ matrix [ row ] [ c ] = char ;
42+ }
43+ }
44+ }
45+ } ;
You canโt perform that action at this time.
0 commit comments