matrix is a grid of integers with m rows and n columns. Wherever the grid holds a 0, clear that cell's whole row and whole column: every cell in them becomes 0.
Only the zeros that were in the grid at the start spread. A cell you turn into 0 along the way doesn't clear its own row and column.
Change matrix itself. Your function returns nothing; the answer judged is matrix after your function has run.
The one 0 sits in the second row and second column, so both of those are cleared. Everything else keeps its value.
The starting zeros are in the first row (third column) and the second row (first column). Those two rows and those two columns are cleared. The middle column keeps -3 and 7: the zeros written into it came from clearing, not from the start.
With a single row, one zero clears all of it.
Constraints
m == matrix.lengthn == matrix[i].length1 <= m, n <= 200-2^31 <= matrix[i][j] <= 2^31 - 1Find every starting zero before clearing anything. If you clear as you scan, the zeros you write look like starting zeros and spread across the whole grid. A list of rows to clear and a list of columns to clear is enough.
A cell ends up 0 exactly when its row or its column held a zero at the start. That's the whole rule. So the answer depends on two short lists of facts: which rows had a starting zero, and which columns did.
Time O(m · n)Space O(1)
The full walkthrough and code are part of Premium