FrontendX
Set Matrix Zeroes
medium
Blind solve
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
1
/**
2
* @param {number[][]} arg1
3
* @return {number[][]}
4
*/
5
function solve(arg1) {
6
// solve Set Matrix Zeroes problem here
7
return [];
8
}
Click to start editing · javascript
Set Matrix Zeroes
Given a matrix, if an element is 0, set its entire row and column to 0. Return the resulting matrix.