-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathspiral-matrix.py
More file actions
24 lines (17 loc) · 784 Bytes
/
spiral-matrix.py
File metadata and controls
24 lines (17 loc) · 784 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
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix or not matrix[0]:
return []
visited = [[False for _ in matrix[0]] for _ in matrix]
result = []
row, col = 0, 0
vec = (0, 1)
while row != len(matrix) and not visited[row][col]:
visited[row][col] = True
result.append(matrix[row][col])
row, col = tuple(map(lambda x, y: x + y, (row, col), vec))
if row == len(matrix) or col == len(matrix[0]) or visited[row][col]:
row, col = tuple(map(lambda x, y: x - y, (row, col), vec))
vec = (vec[1], - vec[0])
row, col = tuple(map(lambda x, y: x + y, (row, col), vec))
return result