-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ54.py
More file actions
47 lines (46 loc) · 1.25 KB
/
Q54.py
File metadata and controls
47 lines (46 loc) · 1.25 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
42
43
44
45
46
47
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if len(matrix) == 0:
return []
toL,toR,toU,toD = False,True,False,False
left,right,top,bottom = 0,len(matrix[0]),0,len(matrix)
count = 0
resultList = []
i,j = 0,0
while count<len(matrix)*len(matrix[0]):
resultList.append(matrix[i][j])
count+=1
i+=toD
i-=toU
j+=toR
j-=toL
if j>right-1:
j-=1
i+=1
toR = False
toD = True
top+=1
continue
if i>bottom-1:
i-=1
j-=1
toD = False
toL = True
right-=1
continue
if j<left:
j+=1
i-=1
toL = False
toU = True
bottom-=1
continue
if i<top:
i+=1
j+=1
toU = False
toR = True
left+=1
continue
return resultList