Given a matrix, A of size M x N of 0s and 1s. If an element is 0, set its entire row and column to 0.
Input 1:
[ [1, 0, 1],
[1, 1, 1],
[1, 1, 1] ]
Output 1:
[ [0, 0, 0],
[1, 0, 1],
[1, 0, 1] ]
My Code:
def setZeroes(self, A):
B=A
for i in range(len(A)):
for j in range(len(A[i])):
if(A[i][j]==0):
for x in range(len(A[i])):
B[i][x]=0
for y in range(len(A)):
B[y][j]=0
A=B
return A
Is creating a zero matrix. What do I miss ?
import copy
def setZeroes(A):
B=copy.deepcopy(A)
for i in range(len(A)):
for j in range(len(A[i])):
if(A[i][j]==0):
for x in range(len(A[i])):
B[i][x]=0
for y in range(len(A)):
B[y][j]=0
return B
A = [ [1, 0, 1],
[1, 1, 1],
[1, 1, 1] ]
print(setZeroes(A))
since, array is pass by reference, you have to deepcopy it.