Skip to content

Commit f5cde09

Browse files
Create taurus09318976.py
1 parent 0ad15e0 commit f5cde09

File tree

1 file changed

+71
-0
lines changed

1 file changed

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

Comments
Β (0)