首页 > 代码库 > [LintCode] Set Matrix Zeros
[LintCode] Set Matrix Zeros
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Given a matrix
[
[1,2],
[0,3]
],
return
[
[0,2],
[0,0]
]
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
An obvious solution is to simply iterate through all 0 entries and set each 0 entry‘s row and column to all 0s. However, it is not correct as shown
in the following counter example.
[
[1, 0 , 1],
[1, 1, 1],
[1, 1, 1]
]
Scan the first row and found a 0, change all entries on row 0 and column 1 to 0. When we scan the 2nd and 3rd rows, we should not make any changes
as there is no 0 in the original matrix. But since we‘ve already changed all entries on column 1 to 0, this result in an incorrect result of all 0s.
The expected result should be
[
[0, 0 , 0],
[1, 0, 1],
[1, 0, 1]
]
The problem here is that the transition should happen at all 0 entries at the same time instantly. If we do it one entry at a time without keeping which rows and columns
have 0s in the original matrix, we will not get the right result. For the same reason, if we try to use bfs by putting all 0s into a queue and expand layer by layer, it will
fail.
Solution 1. O(m * n) runtime, O(m + n) space
1.Since we only care about if a row or column has any zeros or not, we can use two boolean arrays of size m and n respectively.
They are used to keep track if a row or column has any zeros.
2.Set a row or column to all 0s based on the values in the two flag arrays.
1 public class Solution { 2 public void setZeroes(int[][] matrix) { 3 if(matrix == null || matrix.length == 0 || matrix[0].length == 0){ 4 return; 5 } 6 boolean[] row = new boolean[matrix.length]; 7 boolean[] col = new boolean[matrix[0].length]; 8 for(int i = 0; i < matrix.length; i++){ 9 for(int j = 0; j < matrix[0].length; j++){ 10 if(matrix[i][j] == 0){ 11 row[i] = true; 12 col[j] = true; 13 } 14 } 15 } 16 for(int i = 0; i < matrix.length; i++){ 17 if(row[i]){ 18 for(int j = 0; j < matrix[0].length; j++){ 19 matrix[i][j] = 0; 20 } 21 } 22 } 23 for(int j = 0; j < matrix[0].length; j++){ 24 if(col[j]){ 25 for(int i = 0; i < matrix.length; i++){ 26 matrix[i][j] = 0; 27 } 28 } 29 } 30 } 31 }
Runtime: O(m * n), BCR, since in the worst case roughly m * n number of entries need to be changed to 0.
Space: O(m + n), Can we do better with space usage? Can we use O(1) space?
Solution 2. O(m * n) runtime, O(1) space
In solution 1, we used two extra arrays, one is size of m, the other is size of n. If we don‘t use extra memory
to store which rows/columns zero information, then we need to store them in the input matrix. Let‘s pick the
first row and the first column for this purpose.
The alogrithm works as follows:
1. Check if the 1st row and 1st column have any 0s, and set variable rowZero and colZero.
2. Iterate through the rest of the matrix, setting matrix[i][0] and matrix[0][j] to 0 whenever there is a 0 in matrix[i][j].
3. Iterate through the rest of matrix by row, set row i to all 0s if matrix[i][0] == 0.
4. Iterate through the rest of matrix by column, set column j to all 0s if matrix[0][j] == 0.
5. Set the first row and first column to 0s if necessary.
1 public class Solution { 2 public void setZeroes(int[][] matrix) { 3 if(matrix == null || matrix.length == 0 || matrix[0].length == 0){ 4 return; 5 } 6 boolean rowZero = false; 7 for(int j = 0; j < matrix[0].length; j++){ 8 if(matrix[0][j] == 0){ 9 rowZero = true; 10 break; 11 } 12 } 13 boolean colZero = false; 14 for(int i = 0; i < matrix.length; i++){ 15 if(matrix[i][0] == 0){ 16 colZero = true; 17 break; 18 } 19 } 20 for(int i = 1; i < matrix.length; i++){ 21 for(int j = 1; j < matrix[0].length; j++){ 22 if(matrix[i][j] == 0){ 23 matrix[i][0] = 0; 24 matrix[0][j] = 0; 25 } 26 } 27 } 28 for(int i = 1; i < matrix.length; i++){ 29 if(matrix[i][0] == 0){ 30 for(int j = 1; j < matrix[0].length; j++){ 31 matrix[i][j] = 0; 32 } 33 } 34 } 35 for(int j = 1; j < matrix[0].length; j++){ 36 if(matrix[0][j] == 0){ 37 for(int i = 1; i < matrix.length; i++){ 38 matrix[i][j] = 0; 39 } 40 } 41 } 42 if(rowZero){ 43 for(int j = 0; j < matrix[0].length; j++){ 44 matrix[0][j] = 0; 45 } 46 } 47 if(colZero){ 48 for(int i = 0; i < matrix.length; i++){ 49 matrix[i][0] = 0; 50 } 51 } 52 } 53 }
Related Problems
Rotate Image
[LintCode] Set Matrix Zeros