-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0054M. Spiral Matrix.py
More file actions
41 lines (40 loc) · 1.29 KB
/
0054M. Spiral Matrix.py
File metadata and controls
41 lines (40 loc) · 1.29 KB
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
#Runtime: 20 ms, faster than 99.24% of Python3 online submissions for Spiral Matrix.
#Memory Usage: 14 MB, less than 95.03% of Python3 online submissions for Spiral Matrix.
class Solution:
def spiralOrder(self, matrix):
i = 0
j = 0
list_n = []
direct = 'r'
frame = [0, len(matrix[0]) - 1, len(matrix) - 1, 0]
while frame[2] - frame[0] >= 0 and frame[1] - frame[3] >= 0:
list_n.append(matrix[i][j])
if direct == 'r':
if j != frame[1]:
j += 1
else:
direct = 'd'
i += 1
frame[0] += 1
elif direct == 'd':
if i != frame[2]:
i += 1
else:
direct = 'l'
j -= 1
frame[1] -= 1
elif direct == 'l':
if j != frame[3]:
j -= 1
else:
direct = 'u'
i -= 1
frame[2] -= 1
elif direct == 'u':
if i != frame[0]:
i -= 1
else:
direct = 'r'
j += 1
frame[3] += 1
return list_n