219. Set Matrix Zeroes

Medium
Matrix Marker Compression
Array

Problem

Given an m x n integer matrix, if an element is 0, set its entire row and column to 0. Return the resulting matrix.

Examples

Example 1

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

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]]

Example 3

Input: [[1]]
Output: [[1]]
Constraints

1 <= matrix.length <= 200

1 <= matrix[i].length <= 200

Hints

?? Use the first row and first column as marker storage.

?? Track separately whether the first row or first column originally contained zero.

Expected Complexity
Time: O(m * n)
Space: O(1)
Follow-up

Can you solve it using O(1) auxiliary space?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[[1,1,1],[1,0,1],[1,1,1]]
Expected
[[1,0,1],[0,0,0],[1,0,1]]