Skip to the code
+25 XP

#80 Clear Rows and Columns

medium

In a spreadsheet-style puzzle, a 0 in any cell wipes out its whole row and its whole column.

Write clearZeros(grid) that changes grid in place: every row and every column that held a 0 in the original grid becomes all zeros. Zeros created by this step don't wipe out anything further.

Example 1

Input: grid = [[1,2,3],[4,0,6],[7,8,9]]
Output: [[1,0,3],[0,0,0],[7,0,9]]

The 0 in the middle clears row 1 and column 1.

Example 2

Input: grid = [[0,1],[1,1]]
Output: [[0,0],[0,1]]

Row 0 and column 0 are cleared; the bottom-right cell stays.

Constraints

  • 1 <= rows, cols <= 10^5
  • rows * cols <= 10^6
  • -2^31 <= grid[r][c] <= 2^31 - 1
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).