Python O(1) space and O(n2) time solution


#1

def setZeroes(self, A):
fcz = False
for r in range(len(A)):
if A[r][0] == 0:
fcz = True
for r in range(len(A)):
for c in range(1, len(A[0])):
if A[r][c] == 0:
A[0][c] = 0
A[r][0] = 0
for c in range(1, len(A[0])):
if A[0][c] == 0:
for r in range(len(A)):
A[r][c] = 0

    for r in range(len(A)):
        if A[r][0] == 0:
            A[r] = [0] * len(A[0])
    if fcz:
        for r in range(len(A)):
            A[r][0] = 0
    return A

basic idea is to use first column and row to store the information if a row or col should be zero. Also there will be a conflict at r=0, c=0 as it might mean row is zero or col is zero or both, so this is handled. Also sequence matters.