-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path02_spiral_matrix.py
More file actions
57 lines (39 loc) · 1.24 KB
/
02_spiral_matrix.py
File metadata and controls
57 lines (39 loc) · 1.24 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
48
49
50
51
52
53
54
55
56
57
class Solution:
def spiralOrder(self, matrix: list[list[int]]) -> list[int]:
res = []
left, right = 0, len(matrix[0])
top, bottom = 0, len(matrix)
while left < right and top < bottom:
# get every i in the top row
for i in range(left, right):
res.append(matrix[top][i])
top += 1
# get every i in the right col
for i in range(top, bottom):
res.append(matrix[i][right - 1])
right -= 1
if not (left < right and top < bottom):
break
# get every i in the bottom row
for i in range(right - 1, left - 1, -1):
res.append(matrix[bottom - 1][i])
bottom -= 1
# get every i in the left col
for i in range(bottom - 1, top - 1, -1):
res.append(matrix[i][left])
left += 1
return res
if __name__ == "__main__":
obj = Solution()
matrix1 = [
[1,2,3],
[4,5,6],
[7,8,9]
]
print(obj.spiralOrder(matrix=matrix1))
matrix2 = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
print(obj.spiralOrder(matrix=matrix2))