/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 any element grid[i][j] is zero, set all elements in row i and column j to zero. 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: Element at (1,1) is zero, so row 1 and column 1 are set to zero.

Example 2

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

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

Explanation: Element at (1,0) is zero, so row 1 and column 0 are set to zero.

Constraints

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

Case 1

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

Expected: [[0,0,0],[2,3,0],[6,7,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,2],[3,4]]

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

Case 4

Input: [[1]]

Expected: [[1]]

Case 5

Input: [[0]]

Expected: [[0]]