76. Set Matrix Zeroes

MediumArrayArray

Given an m x n integer matrix, if an element is 0, set its entire row and column to 0s. Do this in-place.

Input: A 2D integer array matrix of size m x n.

Output: The matrix with zeros propagated to full rows and columns.

Examples

Example 1
Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Explanation: Element at (1,1) is 0. Set row 1 and col 1 to zeros.
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: (0,0)=0→row0,col0 zeroed. (0,3)=0→row0,col3 zeroed.
Example 3
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,2,3],[4,5,6],[7,8,9]]
Explanation: No zeros — matrix unchanged.

Constraints

Asked by

BloombergMicrosoftMetaAmazonGoogleInfosys
Solve this problem in the editor →