Skip to content

Commit 540aa73

Browse files
authored
Set_Matrix_Zeroes
Initial File
1 parent 043a387 commit 540aa73

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Set_Matrix_Zeroes

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def setZeroes(self, matrix):
2+
"""
3+
:type matrix: List[List[int]]
4+
:rtype: None Do not return anything, modify matrix in-place instead.
5+
"""
6+
first_Row=False
7+
first_Col=False
8+
9+
#Check if first column has any zeroes
10+
for i in range(0,len(matrix)):
11+
if(matrix[i][0] ==0):
12+
first_Col = True
13+
14+
# Check if first row has any zeroes
15+
for i in range(0,len(matrix[0])):
16+
if(matrix[0][i] ==0):
17+
first_Row = True
18+
19+
#Check rest of matrix for zeros,mark the row and column using first row and column
20+
for i in range(1,len(matrix)):
21+
for j in range(1,len(matrix[0])):
22+
if(matrix[i][j] ==0):
23+
matrix[0][j]=0
24+
matrix[i][0]=0
25+
26+
#Zerofy the marked Columns and Rows
27+
for i in range(1,len(matrix)):
28+
if(matrix[i][0]==0):
29+
for j in range(1,len(matrix[0])):
30+
matrix[i][j]=0
31+
32+
for i in range(1,len(matrix[0])):
33+
if(matrix[0][i]==0):
34+
for j in range(1,len(matrix)):
35+
matrix[j][i]=0
36+
37+
# Zerofy first Column and Row if necessary
38+
if first_Row:
39+
for i in range(0, len(matrix[0])):
40+
matrix[0][i] = 0
41+
42+
if first_Col:
43+
for i in range(0, len(matrix)):
44+
matrix[i][0] = 0
45+
46+
return matrix

0 commit comments

Comments
 (0)