Spiral matrix iii

class Solution:
def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int):
total=rows*cols
r0,c0=rStart,cStart
h_unit,v_unit=1,1
result=[]
result.append([r0,c0])
#print(result)
while(len(result)<total):
count=1
while(count<=h_unit):
c0+=1
if(self.check_inside(r0,c0,rows,cols)):
result.append([r0,c0])
count+=1
h_unit+=1
count=1
while(count<=v_unit):
r0+=1
if(self.check_inside(r0,c0,rows,cols)):
result.append([r0,c0])
count+=1
count=1
v_unit+=1
while(count<=h_unit):
c0-=1
if(self.check_inside(r0,c0,rows,cols)):
result.append([r0,c0])
count+=1
count=1
h_unit+=1
while(count<=v_unit):
r0-=1
if(self.check_inside(r0,c0,rows,cols)):
result.append([r0,c0])
count+=1
v_unit+=1
return(result)
def check_inside(self,r0,c0,rows,cols):
if((0<=r0 and r0<rows) and (0<=c0 and c0<cols) ):
return True
return False
class Solution:
def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int):
total=rows*cols
r0,c0=rStart,cStart
direction=[0,1,0,-1,0]
result=[[r0,c0]]
len_,curr_direction=0,0
while(len(result)<total):
if(curr_direction==0 or curr_direction==2 ):
len_+=1
for k in range(0,len_):
r0+=direction[curr_direction]
c0+=direction[curr_direction+1]
if(self.check_inside(r0,c0,rows,cols)):
result.append([r0,c0])
curr_direction+=1
curr_direction%=4
return(result)
def check_inside(self,r0,c0,rows,cols):
if((0<=r0 and r0<rows) and (0<=c0 and c0<cols) ):
return True
return False

--

--

--

More from Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.