-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ54.py
More file actions
31 lines (30 loc) · 865 Bytes
/
Q54.py
File metadata and controls
31 lines (30 loc) · 865 Bytes
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
class Solution:
def __init__(self):
pass
def spiralOrder(self, matrix):
row = len(matrix)
if not row:
return []
column = len(matrix[0])
if not column:
return []
dr = [0, 1, 0, -1]
dc = [1, 0, -1, 0]
r = c = 0
di = 0
res = []
seen = [[False] * column for _ in range(row)]
for _ in range(row * column):
seen[r][c] = True
res.append(matrix[r][c])
cr = r + dr[di]
cc = c + dc[di]
if 0 <= cr < row and 0 <= cc < column and not seen[cr][cc]:
r, c = cr, cc
else:
di = (di + 1) % 4
r, c = r + dr[di], c + dc[di]
return res
if __name__ == '__main__':
so = Solution()
so.spiralOrder([[1,2,3],[4,5,6],[7,8,9]])