-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path054_spiral-matrix.py
50 lines (47 loc) · 1.44 KB
/
054_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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@author: rye
#@time: 2019/3/16
'''
这道题花了比较长的时间想,现在脑子有点晕
'''
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if matrix == []:
return []
res = []
left = up = 0
maxRight = len(matrix[0]) - 1
maxDown = len(matrix) - 1
direction = 0 # 0 is right, 1 is down, 2 is left, 3 up
while True:
if direction == 0:
for i in range(left, maxRight + 1):
res.append(matrix[up][i])
up += 1
elif direction == 1:
for i in range(up, maxDown + 1):
res.append(matrix[i][maxRight])
maxRight -= 1
elif direction == 2:
for i in reversed(range(left, maxRight + 1)):
res.append(matrix[maxDown][i])
maxDown -= 1
else:
for i in reversed(range(up, maxDown+1)):
res.append(matrix[i][left])
left += 1
if up > maxDown or left > maxRight:
return res
direction = (direction + 1) % 4
if __name__ == '__main__':
matrix = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
print(Solution().spiralOrder(matrix))