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+ """
2+ ํ์ด :
3+ ์ค๋ฅธ์ชฝ, ์๋์ชฝ์ผ๋ก ์ด๋ํ ๋ ๊ฐ๊ฐ column๊ณผ row๊ฐ ์ฆ๊ฐํ๋ฏ๋ก direction 1
4+ ์ผ์ชฝ, ์์ชฝ์ ๊ฐ์ํ๋ฏ๋ก direction -1
5+ ๋ก ์ค์ ํ๊ณ n_row ๋๋ n_col๋งํผ direction์ ์ด๋ํ๋ฉด์ ans์ ๋ด์์ค๋ค
6+ ๊ฐ for๋ฌธ์ด ๋๋ ๋๋ง๋ค n_row ๋๋ n_col์ ๊ฐ์์์ผ ์ฃผ๊ณ
7+ ๋ ์ค ํ๋๊ฐ 0์ด ๋ ๋๊น์ง ๊ณ์ ์งํ
8+
9+ - col์ด -1๋ถํฐ ์์ํด์ matrix[0][0]๋ถํฐ appendํ ์ ์๋๋ก ์ ์
10+ - n_cols, n_rows ๋ ์ค ํ๋๋ง 0์ด๋๋ ๋๋จ์ ์ ์
11+
12+ TC : O(M * N)
13+ matrix ์ ์ฒด๋ฅผ ํ๋ฒ์ฉ ์ํํด์ผํ๋ฏ๋ก
14+
15+ SC : O(1)
16+ returnํ ๋ฆฌ์คํธ ์ธ์๋ ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ด ์์๊ฐ(5๊ฐ)์ด๋ฏ๋ก
17+ """
18+
19+ class Solution :
20+ def spiralOrder (self , matrix : List [List [int ]]) -> List [int ]:
21+ ans = []
22+ n_rows = len (matrix )
23+ n_cols = len (matrix [0 ])
24+
25+ row = 0
26+ col = - 1
27+ direction = 1
28+
29+ while n_cols and n_rows :
30+ for _ in range (n_cols ):
31+ col += direction
32+ ans .append (matrix [row ][col ])
33+ n_rows -= 1
34+
35+ for _ in range (n_rows ):
36+ row += direction
37+ ans .append (matrix [row ][col ])
38+ n_cols -= 1
39+
40+ direction *= - 1
41+
42+ return ans
You canโt perform that action at this time.
0 commit comments