Skip to content

Commit fc2f92c

Browse files
committed
885
1 parent ef038fd commit fc2f92c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Aug-8-24.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]:
3+
l,r,t,b = cStart,cStart+1,rStart,rStart+1
4+
res = []
5+
vis = [[False for i in range(cols)] for j in range(rows)]
6+
while l>=0 or r<cols or t>=0 or b<rows:
7+
# l -> r
8+
for i in range(l,r+1):
9+
if 0<=t<rows and 0<=i<cols and not vis[t][i]:
10+
vis[t][i] = True
11+
res.append([t,i])
12+
l -= 1
13+
14+
# t -> b
15+
for i in range(t,b+1):
16+
if 0<=i<rows and 0<=r<cols and not vis[i][r]:
17+
vis[i][r] = True
18+
res.append([i,r])
19+
t -= 1
20+
21+
# r -> l
22+
for i in range(r,l,-1):
23+
if 0<=b<rows and 0<=i<cols and not vis[b][i]:
24+
vis[b][i] = True
25+
res.append([b,i])
26+
r += 1
27+
28+
# b -> t
29+
for i in range(b,t,-1):
30+
if 0<=i<rows and 0<=l<cols and not vis[i][l]:
31+
vis[i][l] = True
32+
res.append([i,l])
33+
b += 1
34+
return res

0 commit comments

Comments
 (0)