forked from subhalaha131/python-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetMatrixZero.py
More file actions
35 lines (29 loc) · 1.2 KB
/
SetMatrixZero.py
File metadata and controls
35 lines (29 loc) · 1.2 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
class SetMatrixZeros:
def setZeroes(self, matrix):
self.matrixZeros(matrix)
def matrixZeros(self, arr):
firstCol = False
firstRow = False
# Step 1: check for zeros and mark in the first row/col
for i in range(len(arr)):
for j in range(len(arr[0])):
if arr[i][j] == 0:
if j == 0:
firstCol = True
if i == 0:
firstRow = True
arr[i][0] = 0
arr[0][j] = 0
# Step 2: make rows and cols zeros if they have a zero, except first row/col
for i in range(1, len(arr)):
for j in range(1, len(arr[0])):
if arr[i][0] == 0 or arr[0][j] == 0:
arr[i][j] = 0
# Step 3: making the first row and first col zero if required
# at least one element is zero in the first row: therefore making all zero
if firstRow:
arr[0] = [0] * len(arr[0])
# at least one element is zero in the first col: therefore making all zero
if firstCol:
for i in range(len(arr)):
arr[i][0] = 0