File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def spiralOrder (self , matrix : List [List [int ]]) -> List [int ]:
3
+
4
+ answer = []
5
+
6
+ # κ²½κ³κ°
7
+ top , bottom = 0 , len (matrix )- 1
8
+ left , right = 0 , len (matrix [0 ])- 1
9
+
10
+ # μκ³λ°©ν₯μΌλ‘ ν λ°ν΄μ© λκΈ°
11
+ while top <= bottom and left <= right :
12
+
13
+ # μ€λ₯Έμͺ½ μ΄λ
14
+ for i in range (left ,right + 1 ):
15
+ answer .append (matrix [top ][i ])
16
+ top += 1
17
+
18
+ # μλλ‘ μ΄λ
19
+ for i in range (top ,bottom + 1 ):
20
+ answer .append (matrix [i ][right ])
21
+ right -= 1
22
+
23
+ # μΌμͺ½ μ΄λ
24
+ if top <= bottom : # μμμ topμ μ¦κ°μμΌ°κΈ° λλ¬Έμ λ€μ κ²μ¬(μμ§ μλμͺ½μ νμ΄ λ¨μμλ κ²½μ°μλ§ μν)
25
+ for i in range (right ,left - 1 ,- 1 ):
26
+ answer .append (matrix [bottom ][i ])
27
+ bottom -= 1
28
+
29
+ # μλ‘ μ΄λ
30
+ if left <= right : # μμμ rightλ₯Ό κ°μμμΌ°κΈ° λλ¬Έμ λ€μ κ²μ¬(μμ§ μΌμͺ½μ μ΄μ΄ λ¨μμλ κ²½μ°μλ§ μν)
31
+ for i in range (bottom ,top - 1 ,- 1 ):
32
+ answer .append (matrix [i ][left ])
33
+ left += 1
34
+
35
+ return answer
You canβt perform that action at this time.
0 commit comments