SAYAN-2000
/
Must-Do-Coding-Questions-For-Service-Based-Companies-and-Product-Based-Companies.
Public
forked from rohitjila/Must-Do-Coding-Questions-For-Service-Based-Companies-and-Product-Based-Companies.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet Matrix Zero
40 lines (30 loc) · 1.13 KB
/
Set Matrix Zero
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
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
rows=len(matrix)
col=len(matrix[0])
row_zero=False
col_zero=False
for i in range(rows):
if matrix[i][0] == 0:
row_zero=True
for j in range(col):
if matrix[0][j] == 0:
col_zero=True
for i in range(1,rows):
for j in range(1,col):
if matrix[i][j] == 0:
matrix[0][j]=matrix[i][0]=0
for i in range(1,rows):
if matrix[i][0] == 0:
for j in range(1,col):
matrix[i][j] = 0
for j in range(1,col):
if matrix[0][j] == 0:
for i in range(1,rows):
matrix[i][j] = 0
if col_zero:
for j in range(col):
matrix[0][j] = 0
if row_zero:
for i in range(rows):
matrix[i][0] = 0