File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ ํด๋น ๋ฌธ์ ๋ฅผ ํ์ธํ๊ณ
3+ ์๋จ -> ์ฐ์ธก -> ํ๋จ -> ์ข์ธก ๋ฐฉํฅ์ผ๋ก ๋์ ํ์ผ๋ก ์ ๊ทผํ๋ ๊ฒ์ ์ฐ์ ์์๋ก ์๊ฐํ์ต๋๋ค.
4+ pop์ ํตํด์ matrix์ ํ๊ณผ ์ด์ ์ ๊ฑฐํ๋ฉด์ ์ ๊ทผํ๋ ๋ฐฉ์์ ์ฌ์ฉํด ํด๊ฒฐํ์ต๋๋ค.
5+
6+ ์ด๋ ๊ฒ ๋ฌธ์ ๋ฅผ ํ์์๋ pop์ ์ฌ์ฉํ์ฌ ํ๊ณผ ์ด์ ์ ๊ฑฐํ๊ธฐ ๋๋ฌธ์
7+ ๋น์ผ ๊ณ์ฐ ๋น์ฉ์ด ๋ฐ์ํ๋ ๋ถ๋ถ์ ๊ณ ๋ คํ์ง ๋ชปํ๋ค๋ ์ ์ด ์์ฝ๊ฒ ๋๊ปด์ง๋ค์.
8+
9+ Time Complexity: O(mxn)
10+ - m: ํ์ ์๋งํผ ์ ๊ทผ
11+ - n: ์ด์ ์๋งํผ ์ ๊ทผ
12+
13+ Space Complexity: O(mxn)
14+ - result ๋ฆฌ์คํธ๊ฐ ์ ์ฒด ์์๋ฅผ ์ ์ฅ
15+ '''
16+ class Solution :
17+ def spiralOrder (self , matrix : List [List [int ]]) -> List [int ]:
18+ result = []
19+ idx = 0 # ์ฒ์ ๊ฐ
20+ while matrix :
21+ result += matrix .pop (idx )
22+ for i in range (len (matrix )):
23+ if matrix [i ]:
24+ result .append (matrix [i ].pop ())
25+
26+ # ๋น ํ ์ ๊ฑฐ (์ค์!)
27+ matrix = [row for row in matrix if row ]
28+ if not matrix :
29+ break
30+
31+ result += matrix .pop ()[::- 1 ]
32+
33+ for j in range (len (matrix )- 1 , - 1 , - 1 ):
34+ if matrix [j ]:
35+ result .append (matrix [j ].pop (0 ))
36+
37+ return result
You canโt perform that action at this time.
0 commit comments