-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSpiral Matrix.py
32 lines (29 loc) · 1.02 KB
/
Spiral Matrix.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]:
col, row = len(matrix[0]), len(matrix)
l, t, r, b = 0, 0, col - 1, row - 1
res = []
while l <= r and t <= b:
for i in range(l, r):
res.append(matrix[t][i])
for i in range(t, b):
res.append(matrix[i][r])
# Append the orphan left by the open interval
if t == b:
res.append(matrix[t][r])
else:
# From right to left at the bottom
for i in range(r, l, -1):
res.append(matrix[b][i])
# Avoid duplicated appending if it is a square
if l == r and t != b:
res.append(matrix[b][r])
else:
# From bottom to top at the left
for i in range(b, t, -1):
res.append(matrix[i][l])
l += 1
t += 1
r -= 1
b -= 1
return res