-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspiral_order_traversal.py
32 lines (27 loc) · 1.07 KB
/
spiral_order_traversal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix:
return []
sz, sz0 = len(matrix) - 1, len(matrix[0]) - 1
rowBegin, rowEnd, colBegin, colEnd = 0, sz, 0, sz0
ans = []
while rowBegin <= rowEnd and colBegin <= colEnd:
#left to right
for i in range(colBegin, colEnd + 1):
ans.append(matrix[rowBegin][i])
rowBegin += 1
#top to bottom
for i in range(rowBegin, rowEnd + 1):
ans.append(matrix[i][colEnd])
colEnd -= 1
#right to left
if rowBegin <= rowEnd:
for i in range(colEnd, colBegin - 1, -1):
ans.append(matrix[rowEnd][i])
rowEnd -= 1
#bottom to top
if colBegin <= colEnd:
for i in range(rowEnd, rowBegin - 1, -1):
ans.append(matrix[i][colBegin])
colBegin += 1
return ans