-
Notifications
You must be signed in to change notification settings - Fork 612
/
54.py
43 lines (37 loc) · 961 Bytes
/
54.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
'''
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
'''
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if not matrix:
return []
R, C = len(matrix), len(matrix[0])
dr = [0, 1, 0, -1]
dc = [1, 0, -1, 0]
result = []
seen = [[False]*C for _ in range(R)]
row = 0
col = 0
di = 0
for _ in range(R*C):
result.append(matrix[row][col])
seen[row][col] = True
rr, cc = row + dr[di], col + dc[di]
if 0 <= rr < R and 0 <= cc < C and not seen[rr][cc]:
row, col = rr, cc
else:
di = (di+1)%4
row, col = row + dr[di], col + dc[di]
return result