Skip to content
BASA DSA

Set Matrix Zeroes

MediumMath & Geometry

For every zero in the original matrix, set its entire row and column to zero in place. Newly written zeros do not trigger more changes. Return nothing; aim for constant auxiliary space.

Example 1

0,01
0,12
0,23
1,04
1,10
1,26
2,07
2,18
2,29
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
matrix = [[1,2,3],[4,0,6],[7,8,9]]
Output
[[1,0,3],[0,0,0],[7,0,9]]

Explanation

The original zero clears row 1 and column 1; other corners stay unchanged.

Example 2

0,00
0,11
1,02
1,13
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
matrix = [[0,1],[2,3]]
Output
[[0,0],[0,3]]

Explanation

The top-left zero clears row 0 and column 0, leaving 3.

Example 3

0,01
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
matrix = [[1]]
Output
[[1]]

Explanation

No original zero means no change.

Constraints

  • 1 <= rows,columns <= 200
  • Entries are signed 32-bit integers

Function contractsetZeroesArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…