Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Have you met this question in a real interview? Yes Example Given a matrix
[
[1,2],
[0,3]
],
return [ [0,2], [0,0] ]
Challenge 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?
Solution:
The first method used two boolean arrays to store the columns or rows should be set to zero. To make it use constant space, the second method use the first row and column to store the information that rows and columns should be set to zero.
/************************************************************************* > File Name: SetMatrixZeros.java > Author: Yao Zhang > Mail: psyyz10@163.com > Created Time: Sun 15 Nov 16:11:35 2015 ************************************************************************/
publicclassSetMatrixZeros{ // Solution 1, time complexity O(m * n), space complexity O(m + n) publicvoidsetZeroes(int[][] matrix){ int rowLength = matrix.length; int colLength = matrix[0].length; boolean[] zeroRows = newboolean[rowLength]; boolean[] zeroCols = newboolean[colLength];
for (int row = 0; row < rowLength; row++){ for (int col = 0; col < colLength; col++){ if (matrix[row][col] == 0){ zeroRows[row] = true; zeroCols[col] = true; } } }
for (int row = 0; row < zeroRows.length; row++){ if (zeroRows[row]){ for (int col = 0; col< colLength; col++) matrix[row][col] = 0; } }
for (int col = 0; col < zeroCols.length; col++){ if (zeroCols[col]){ for (int row = 0; row < rowLength; row++) matrix[row][col] = 0; } } }
// Solution 2, time complexity O(m * n), space complexity O(1) publicvoidsetZeroes(int[][] matrix){ int rowLength = matrix.length; int colLength = matrix[0].length; boolean firstRowZero = false; boolean firstColZero = false; // Check whether the first row contains zero for (int col = 0; col < colLength; col ++){ if (matrix[0][col] == 0){ firstRowZero = true; break; } } // Check whether the first column contains zero for (int row = 0; row < rowLength; row++){ if (matrix[row][0] == 0){ firstColZero = true; break; } } // set the element in the first row or column to // be zero if the corresponding column or row contains zero for (int row = 1; row < rowLength; row++){ for (int col = 1; col < colLength; col ++){ if (matrix[row][col] == 0){ matrix[0][col] = 0; matrix[row][0] = 0; } } } // Set corresponding rows to be zeors; for (int row = 1; row < rowLength; row++){ if (matrix[row][0] == 0){ for (int col = 1; col < colLength; col ++){ matrix[row][col] = 0; } } } // Set corresponding cols to be zeros for (int col = 1; col < colLength; col++){ if (matrix[0][col] == 0){ for (int row = 1; row < rowLength; row ++){ matrix[row][col] = 0; } } } // Process the first row if (firstRowZero){ for (int col = 0; col < colLength; col ++){ matrix[0][col] = 0; } } // Process the first column if (firstColZero){ for (int row = 0; row < rowLength; row++){ matrix[row][0] = 0; } } } }