1+ '''
2+ μ£Όμ΄μ§ 2μ°¨μ νλ ¬μ λμ ν μμλ‘ λ°ννλ λ¬Έμ μ
3+ 쑰건 : μ->μ€λ₯Έμͺ½->μλ->μΌμͺ½μ λ°λ³΅μ μΌλ‘ μννλ©° λ²μλ₯Ό μ’νκ°
4+
5+ Example 1. μ κ²½μ°
6+ 1 2 3
7+ 4 5 6
8+ 7 8 9
9+ λ¨κ³ | top bottom left right |νμ¬ νλ ¬ μν | λμ μ€λͺ
10+ μμ 0 2 0 2 1 2 3
11+ 4 5 6
12+ 7 8 9 μμͺ½ ν() μ²λ¦¬ ν top=1λ‘ μ¦κ°
13+
14+ 2-1 1 2 0 2 . . .
15+ 4 5 6
16+ 7 8 9 μ€λ₯Έμͺ½ μ΄(μ΄ 2)μ ν 1 μ²λ¦¬: 6 μΆκ°
17+
18+ 2-2 1 2 0 2 . . .
19+ 4 5 6
20+ 7 8 9 μ€λ₯Έμͺ½ μ΄(μ΄ 2)μ ν 2 μ²λ¦¬: 9 μΆκ°
21+
22+ λ 1 2 0 1 . . .
23+ 4 5 6
24+ 7 8 9 μ€λ₯Έμͺ½ μ΄ μ²λ¦¬ λ, right=1λ‘ κ°μ
25+
26+
27+ '''
28+
29+
30+ class Solution :
31+ def spiralOrder (self , matrix : List [List [int ]]):
32+ if not matrix :
33+ return []
34+
35+ result = []
36+ top , bottom = 0 , len (matrix ) - 1
37+ left , right = 0 , len (matrix [0 ]) - 1
38+
39+ while top <= bottom and left <= right :
40+
41+ # μμͺ½ νμ μΌμͺ½ β μ€λ₯Έμͺ½μΌλ‘ μν
42+ for i in range (left , right + 1 ):
43+ result .append (matrix [top ][i ])
44+
45+ # λ€μλΆν° μμͺ½ νμ μλλ‘ ν μΉΈ μ΄λ
46+ top += 1
47+
48+ # μ€λ₯Έμͺ½ μ΄μ μμͺ½ β μλμͺ½λ‘ μν
49+ for i in range (top , bottom + 1 ):
50+ result .append (matrix [i ][right ])
51+
52+ # λ€μλΆν° μ€λ₯Έμͺ½ μ΄μ μΌμͺ½μΌλ‘ ν μΉΈ μ΄λ
53+ right -= 1
54+
55+ # μλμͺ½ νμ μ€λ₯Έμͺ½ β μΌμͺ½μΌλ‘ μν(νμ΄ λ¨μμμ λλ§)
56+ if top <= bottom :
57+ for i in range (right , left - 1 , - 1 ):
58+ result .append (matrix [bottom ][i ])
59+ bottom -= 1
60+
61+ # μ΄μ΄ λ¨μ μμ λλ§ μΌμͺ½ μ΄μ μλμͺ½ β μμͺ½μΌλ‘ μν
62+ if left <= right :
63+ for i in range (bottom , top - 1 , - 1 ):
64+ result .append (matrix [i ][left ])
65+ left += 1
66+
67+ return result
68+
69+
70+
71+
0 commit comments