-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral-order-matrix-i.py
53 lines (34 loc) · 1.21 KB
/
spiral-order-matrix-i.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
45
46
47
48
49
50
51
52
53
class Solution:
# @param A : tuple of list of integers
# @return a list of integers
def spiralOrder(self, A):
m = len(A)
n = len(A[0])
t = 0
b = m - 1
l = 0
r = n - 1
dir = 0
result = []
while t <= b and l <= r:
if dir == 0 :
for i in A[t][l:r + 1]:
result.append(i)
dir += 1
t += 1
elif dir == 1:
for i in A[t:b + 1]:
result.append(i[r])
dir += 1
r -= 1
elif dir == 2 :
for i in A[b][l:r + 1][::-1]:
result.append(i)
dir += 1
b -= 1
elif dir == 3:
for i in A[t:b + 1][::-1]:
result.append(i[l])
dir = 0
l += 1
return result