Skip to content

Commit 52a2ff4

Browse files
committed
spiral-matrix solution
1 parent d1f6b81 commit 52a2ff4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

β€Žspiral-matrix/yyyyyyyyyKim.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

0 commit comments

Comments
Β (0)