-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set Matrix Zeroes
42 lines (36 loc) · 1.04 KB
/
Set Matrix Zeroes
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def solve(matrix):
m = len(matrix[0])
n = len(matrix)
flag_row,flag_column = False ,False
#flag marking
for i in range(n):
if matrix[i][0]==0:
flag_column = True
break
for j in range(m):
if matrix[0][j]==0:
flag_row = True
break
#mark zero on edges
for i in range(1,n):
for j in range(m):
if matrix[i][j]==0:
matrix[0][j]=0
matrix[i][0]=0
#painting zero marked row and columns
for i in range(1,n):
if matrix[i][0]==0:
for a in range(1,m):
matrix[i][a]=0
for j in range(m):
if matrix[0][j]==0:
for a in range(1,n):
matrix[a][j]=0
#painting first raw and column if flag is on
if flag_column==True:
for i in range(n):
matrix[i][0]=0
if flag_row==True:
for j in range(m):
matrix[0][j]=0
return matrix