/73. Set Matrix Zeroes

73. Set Matrix Zeroes

Medium
Arrays62.5% acceptance

Given a 2D integer array grid of dimensions m x n, modify grid in-place such that if grid[i][j] == 0, then all elements in row i and column j are set to 0. The operation must be performed in-place without using additional space proportional to the size of the matrix.

Example 1

Input: [[2,3,4],[5,0,6],[7,8,9]]

Output: [[2,0,4],[0,0,0],[7,0,9]]

Explanation: The element at grid[1][1] is 0, so row 1 and column 1 are set to 0.

Example 2

Input: [[1,2],[0,3]]

Output: [[0,2],[0,0]]

Explanation: grid[1][0] is 0, so row 1 and column 0 are set to 0.

Constraints

  • 1 <= len(grid) <= 200
  • 1 <= len(grid[0]) <= 200
  • -231 <= grid[i][j] <= 231 - 1
  • grid is a rectangular matrix (all rows have equal length)
Python (current runtime)

Case 1

Input: [[4,5,0],[1,2,3],[0,6,7]]

Expected: [[0,0,0],[0,2,0],[0,0,0]]

Case 2

Input: [[1,2,3],[4,5,6],[7,8,9]]

Expected: [[1,2,3],[4,5,6],[7,8,9]]

Case 3

Input: [[0]]

Expected: [[0]]

Case 4

Input: [[1,0],[2,3],[4,5]]

Expected: [[0,0],[2,0],[4,0]]