Skip to content

Commit b942db6

Browse files
committed
spiral-matrix solution
1 parent 182e596 commit b942db6

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

spiral-matrix/hyeri0903.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution:
2+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
3+
'''
4+
1.문제: 나선형으로 numbers return
5+
2.조건
6+
- m, n 길이 최소 = 1. 최대 = 10
7+
- 원소 값 최소 = -100, 최대 = 100
8+
3.풀이
9+
- 마지막 컬럼에 오면 index j change
10+
'''
11+
12+
if len(matrix) == 1 and len(matrix[0]) == 1:
13+
return [matrix[0][0]]
14+
15+
left = 0
16+
right = len(matrix[0]) - 1
17+
top = 0
18+
bottom = len(matrix) - 1
19+
result = []
20+
21+
while top <= bottom and left <= right:
22+
#left -> right
23+
for i in range(left, right + 1):
24+
result.append(matrix[top][i])
25+
top += 1
26+
27+
#top -> bottom
28+
for i in range(top, bottom + 1):
29+
result.append(matrix[i][right])
30+
right -= 1
31+
32+
#right -> left
33+
if top <= bottom:
34+
for i in range(right, left - 1, -1):
35+
result.append(matrix[bottom][i])
36+
bottom -= 1
37+
38+
#bottom -> top
39+
if left <= right:
40+
for i in range(bottom, top - 1, -1):
41+
result.append(matrix[i][left])
42+
left += 1
43+
return result

0 commit comments

Comments
 (0)