-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_SetZerosMatrix.py
More file actions
36 lines (31 loc) · 1.09 KB
/
04_SetZerosMatrix.py
File metadata and controls
36 lines (31 loc) · 1.09 KB
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
# Question link - https://leetcode.com/problems/set-matrix-zeroes/description/?envType=study-plan-v2&envId=top-interview-150
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
m = len(matrix[0])
# let variables
col0 = 1
for i in range(n):
for j in range(m):
if matrix[i][j] == 0:
matrix[i][0] = 0
if j!= 0:
matrix[0][j] = 0
else:
col0 = 0
# Marks the 0 from (1,1) to (n-1) (m-1)
for i in range(1,n):
for j in range(1,m):
if matrix[i][j] != 0:
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
if matrix[0][0] == 0:
for j in range(m):
matrix[0][j] = 0
if col0 == 0:
for i in range(n):
matrix[i][0] = 0
return matrix