FrontendX
Set Matrix Zeroes
medium
Description
AI Assistance
Solution
Test Cases
Test Result
Submissions
Canvas
Set Matrix
Zeroes
Given a 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]]
Hints
Hint 1
Use two boolean arrays to mark rows and columns that need to be zeroed, then iterate through the matrix to apply the zeroing.
Hint 2
Optimize space by using the first row and column of the matrix itself to store the zeroing information instead of additional arrays.
Hint 3
Handle the first row and column separately to avoid overwriting their zeroing information, then apply the zeroing based on the stored information.
Related Problems
two-sum
Set Matrix Zeroes
Given a matrix, if an element is 0, set its entire row and column to 0. Return the resulting matrix.