Set Matrix Zeroes
Given an m x n
integer matrix matrix
, if an element is 0
, set its entire row and column to 0
's.
Example 1:
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Here, we need to traverse matrix and Update element to ZERO, if there is a ZERO in its column and rows.
Brute force approach - Traverse Matrix and check that particular index’s column and rows contains any zero or not.
Instead of searching for every rows and columns, This is better approach if we make two array which contains information that this particular row / column contains zero or not.
Using above loops, we can get to know that these rows and columns have zeros.
After that we’ll traverse the whole matrix and update the matrix element by two conditions : row contains zero , column contains zero.
That’s it for the today. If you have any doubt, feel free to raise query via comment section.