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

--

--

--

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.

Recommended from Medium

Exception In Java

Which is better between CSS grid and CSS flex?

Magento 2: How to Add a Custom Attribute to The Customer’s Address

Field Note Assignment #7

Test Pilot Tracking Protection Graduation Report

Project Management Tools

01B Stackoverflow Survey — Data Understanding

Why Open Source Software Needs Non-Technical Roles

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

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

More from Medium

What if the Monty Hall problem had 10,000 doors?

The Monty Hall Problem for Dummies

Racial Bias in Face Verification: Where do we stand?

Face detection using MTCNN

“Solving” Wordle