Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-088

Set Matrix Zeroes

Problem

Return a copy of matrix where every row and column that contains a 0 is filled with zeros.

Record all zero rows and zero columns in sets during a first pass, then build the result so untouched cells keep their original values. The input matrix must not be modified.

Hint

Collect all zero positions before writing anything, otherwise newly written zeros spread incorrectly.

Test cases

case 1
in: [[[1,1,1],[1,0,1],[1,1,1]]]
expected: [[1,0,1],[0,0,0],[1,0,1]]
case 2
in: [[[0,1,2,0],[3,4,5,2],[1,3,1,5]]]
expected: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
case 3
in: [[[1,2],[3,4]]]
expected: [[1,2],[3,4]]
case 4
in: [[[0]]]
expected: [[0]]
Starter codePython
def set_matrix_zeroes(matrix):
    # Your code here
    pass

4 test cases · run your code against them in the editor