-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
33 lines (27 loc) · 934 Bytes
/
solution.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
class Solution(object):
def spiralMatrixIII(self, R, C, r0, c0):
"""
:type R: int
:type C: int
:type r0: int
:type c0: int
:rtype: List[List[int]]
"""
res = []
directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
direction_index = 0
max_step = 1
cur_step = 0
while len(res) < R * C:
if 0 <= r0 < R and 0 <= c0 < C:
res.append([r0, c0])
if cur_step == max_step:
direction_index = (direction_index + 1) % 4
elif cur_step == max_step * 2:
direction_index = (direction_index + 1) % 4
max_step += 1
cur_step = 0
r0 += directions[direction_index][0]
c0 += directions[direction_index][1]
cur_step += 1
return res