Ctrl K

Zero Out the Crosses

Medium
You are given a rectangular grid of integers grid of size m × n. For every zero that was in the grid at the start, set its whole row and whole column to zero. Zeros created along the way do not spread any further. Return the grid after the zeroing. Try to use only O(1) extra memory.
Example 1
grid=[[5, 7, 2], [4, 0, 9], [8, 6, 3]] → [[5, 0, 2], [0, 0, 0], [8, 0, 3]]

The only zero sits in the centre, so the middle row and the middle column are cleared: [[5, 0, 2], [0, 0, 0], [8, 0, 3]].

Example 2
grid=[[0, 3, 4, 1], [2, 8, 7, 0]] → [[0, 0, 0, 0], [0, 0, 0, 0]]

The zero in the first row clears that row and the first column, the zero in the second row clears that row and the last column. Both rows end up all zeros.

Example 3
grid=[[1, 2], [3, 4]] → [[1, 2], [3, 4]]

There are no zeros, so the grid stays as it is.

Constraints
  • m == grid.length, n == grid[i].length
  • 1 ≤ m, n ≤ 200
  • −2³¹ ≤ grid[i][j] ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumMultiply Mostly-Zero Matrices MediumLonely Dark Pixels MediumDark Pixels in Twin Rows MediumMagic 3×3 Blocks Medium
loading Python…
Input
grid=[[5, 7, 2], [4, 0, 9], [8, 6, 3]]
Expected
[[5, 0, 2], [0, 0, 0], [8, 0, 3]]

Run checks the first 3 tests in your browser. Submit checks all 35 tests on the server, hidden ones included.