forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0054.py
44 lines (38 loc) · 1.18 KB
/
0054.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
33
34
35
36
37
38
39
40
41
42
43
44
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
m = len(matrix)
if m == 0:
return []
n = len(matrix[0])
ans = [0] * (m * n)
count = 0
direction = 0
up = 0
down = m - 1
left = 0
right = n - 1
while True:
if up > down or right < left:
return ans
if direction == 0:
for i in range(left, right + 1):
ans[count] = matrix[up][i]
count += 1
up += 1
elif direction == 1:
for i in range(up, down + 1):
ans[count] = matrix[i][right]
count += 1
right -= 1
elif direction == 2:
for i in range(right, left - 1, -1):
ans[count] = matrix[down][i]
count += 1
down -= 1
else:
for i in range(down, up - 1, -1):
ans[count] = matrix[i][left]
count += 1
left += 1
direction = (direction + 1) % 4
return ans