forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0054-spiral-matrix.py
27 lines (25 loc) · 949 Bytes
/
0054-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
classSolution:
defspiralOrder(self, matrix: List[List[int]]) ->List[int]:
res= []
left, right=0, len(matrix[0])
top, bottom=0, len(matrix)
whileleft<rightandtop<bottom:
# get every i in the top row
foriinrange(left, right):
res.append(matrix[top][i])
top+=1
# get every i in the right col
foriinrange(top, bottom):
res.append(matrix[i][right-1])
right-=1
ifnot (left<rightandtop<bottom):
break
# get every i in the bottom row
foriinrange(right-1, left-1, -1):
res.append(matrix[bottom-1][i])
bottom-=1
# get every i in the left col
foriinrange(bottom-1, top-1, -1):
res.append(matrix[i][left])
left+=1
returnres