102. Set Matrix Zeroes

Medium · Array

Given an m × n matrix, if an element is 0, set its entire row and column to 0 in-place. Do not allocate a new matrix for this operation.

You must modify the matrix directly. The challenge is to do this efficiently without using extra space proportional to the matrix size.

Examples

Example 1
Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Explanation: The element at position (1,1) is 0, so row 1 and column 1 are set to 0.
Example 2
Input: [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
Explanation: Positions (0,0) and (0,3) contain 0, so rows 0 and columns 0 and 3 are set to 0.

Constraints