-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSpiralMatrixPrint.py
More file actions
38 lines (33 loc) · 1 KB
/
SpiralMatrixPrint.py
File metadata and controls
38 lines (33 loc) · 1 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
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix:
return []
T = 0
B = len(matrix) -1
L = 0
R = len(matrix[0]) - 1
dir = 0
ans = []
while T<= B and L <= R:
if dir == 0:
for i in range(L, R + 1):
ans.append(matrix[T][i])
T += 1
elif dir == 1:
for i in range(T, B + 1):
#print(i)
ans.append(matrix[i][R])
R -= 1
elif dir == 2:
for i in range(R, L-1, -1):
print(i)
ans.append(matrix[B][i])
B-=1
elif dir == 3:
for i in range(B, T-1, -1):
ans.append(matrix[i][L])
L += 1
dir += 1
dir %= 4
print(ans)
return ans