-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc_54.spiral-matrix.sol2.py
37 lines (31 loc) · 1.04 KB
/
lc_54.spiral-matrix.sol2.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
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
ans = []
if not matrix:
return ans
t = 0
b = len(matrix) - 1
l = 0
r = len(matrix[0]) - 1
while True:
# moving along the top border all the way to the right
for i in range(l, r+1, 1):
ans.append(matrix[t][i])
t += 1
if t > b: break
# moving along the right border all the way to the bottom
for i in range(t, b+1, 1):
ans.append(matrix[i][r])
r -= 1
if l > r: break
# moving along the bottom border all the way to the left
for i in range(r, l-1, -1):
ans.append(matrix[b][i])
b -= 1
if t > b: break
# moving along the left boarder all the way to the top
for i in range(b, t-1, -1):
ans.append(matrix[i][l])
l += 1
if l > r: break
return ans