-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_885.py
More file actions
27 lines (21 loc) · 890 Bytes
/
LeetCode_885.py
File metadata and controls
27 lines (21 loc) · 890 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
class Solution(object):
def spiralMatrixIII(self, R, C, r0, c0):
ans = [(r0, c0)]
if R * C == 1:
return ans
# For walk length k = 1, 3, 5 ...
for k in range(1, 2*(R+C), 2):
# For direction (dr, dc) = east, south, west, north;
# and walk length dk...
for dr, dc, dk in ((0, 1, k), (1, 0, k), (0, -1, k+1), (-1, 0, k+1)):
# For each of dk units in the current direction ...
for _ in range(dk):
# Step in that direction
r0 += dr
c0 += dc
# If on the grid ...
if 0 <= r0 < R and 0 <= c0 < C:
ans.append((r0, c0))
if len(ans) == R * C:
return ans
print(Solution().spiralMatrixIII(5,6,1,4))