-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm885 v1.py
32 lines (25 loc) · 950 Bytes
/
m885 v1.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
class Solution:
def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]:
output = [[rStart, cStart]]
distance = 1 # current side length
direction = 0 # 0=r, 1=d, 2=l, 3=u
twice = False
while len(output) < rows * cols :
for i in range(1, distance + 1) :
match direction % 4 :
case 0 :
cStart += 1
case 1 :
rStart += 1
case 2 :
cStart -= 1
case 3 :
rStart -= 1
if not (0 <= cStart < cols) or not (0 <= rStart < rows) :
continue
output.append([rStart, cStart])
direction += 1
twice = not twice
if not twice :
distance += 1
return output