Skip to content

Commit 780c0c5

Browse files
committed
adding spiral matrix
1 parent 80d8807 commit 780c0c5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

spiral-matrix/daiyongg-kim.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
3+
res = []
4+
left, right = 0, len(matrix[0])
5+
top, bottom = 0, len(matrix)
6+
7+
while left < right and top < bottom:
8+
for i in range(left, right):
9+
res.append(matrix[top][i])
10+
top += 1
11+
12+
for i in range(top, bottom):
13+
res.append(matrix[i][right - 1])
14+
right -= 1
15+
16+
if not (left < right and top < bottom):
17+
break
18+
19+
for i in range(right -1, left -1, -1):
20+
res.append(matrix[bottom - 1][i])
21+
bottom -= 1
22+
23+
for i in range(bottom - 1, top - 1, -1):
24+
res.append(matrix[i][left])
25+
left += 1
26+
27+
return res

0 commit comments

Comments
 (0)