-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path054_spiralMatrix.py
41 lines (35 loc) · 1.1 KB
/
054_spiralMatrix.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
38
39
40
41
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
m = len(matrix)
n = len(matrix[0])
result = []
aimNumber = m * n
left = 0
right = n-1
top = 0
bottom = m-1
while True:
for col in range(left, right+1):
result.append(matrix[top][col])
top = top + 1
if len(result) == aimNumber:
return result
for row in range(top, bottom+1):
result.append(matrix[row][right])
right = right - 1
if len(result) == aimNumber:
return result
for col in range(right, left-1, -1):
result.append(matrix[bottom][col])
bottom = bottom - 1
if len(result) == aimNumber:
return result
for row in range(bottom, top-1, -1):
result.append(matrix[row][left])
left = left + 1
if len(result) == aimNumber:
return result