1
class Solution:
2
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
3
col, row = len(matrix[0]), len(matrix)
4
l, t, r, b = 0, 0, col - 1, row - 1
5
res = []
6
while l <= r and t <= b:
7
for i in range(l, r):
8
res.append(matrix[t][i])
9
for i in range(t, b):
10
res.append(matrix[i][r])
11

12
# Append the orphan left by the open interval
13
if t == b:
14
res.append(matrix[t][r])
15
else:
16
# From right to left at the bottom
17
for i in range(r, l, -1):
18
res.append(matrix[b][i])
19

20
# Avoid duplicated appending if it is a square
21
if l == r and t != b:
22
res.append(matrix[b][r])
23
else:
24
# From bottom to top at the left
25
for i in range(b, t, -1):
26
res.append(matrix[i][l])
27
l += 1
28
t += 1
29
r -= 1
30
b -= 1
31

32
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0