-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54_Spiral_Matrix.py
30 lines (27 loc) · 947 Bytes
/
54_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
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res= []
if len(matrix) == 0:
return res
m,n = len(matrix),len(matrix[0])
x,y = 0,0
# move is the pattern how the next move will be
move ={
0: [0,1],
1: [1,0],
2: [0,-1],
3: [-1,0]
}
key = 0
for i in range(m*n):
res.append(matrix[x][y])
matrix[x][y] = False
temp_x = x+move[key][0]
temp_y = y+move[key][1]
# everytime time if the next move will cause the x,y out of boundary
# or encounter a number has seen before change the move pattern
if temp_x==m or temp_y==n or matrix[temp_x][temp_y] == False:
key = (key+1)%4
x+=move[key][0]
y+=move[key][1]
return res