https://oj.leetcode.com/problems/set-matrix-zeroes/
http://blog.csdn.net/linhuanmars/article/details/24066199
Public class solution { public void setzeroes (int[][] Matrix) { // Solution A: // setzeroes_noextraspace (Matrix); // Solution B: setzeroes_extrarowandcol (Matrix); } //////////////////////// // solution a: NoExtraSpace // // record using the first row and the first column private void setzeroes_noextraspace (Int[][] matrix) { if (matrix == null) return; // Assume it is m * n; int m = matrix.length; int n = matrix[0].length; // Handle first row boolean firstrow0 = false; for (int i = 0 ; i < n ; i ++) { if ( matrix[0][i] == 0) { firstrow0 = true; } } // first col boolean firstCol0 = false; for (int i = 0 ; i < n ; i ++) { if (matrix[0][i] == 0) { firstCol0 = true; } } for (int i = 1 ; i < m ; i ++) { for (int j = 1 ; j < n ; j ++) { if (matrix[i][j] == 0) { matrix[0][j] = 0; matrix[i][0] = 0; } } } for (int i = 0 ; i < m ; i ++) { for (int j = 0 ; j < n ; j ++) { if (i == 0 && firstcol0) { matrix[i][j] = 0; } else if (j == &NBSP;0&NBSP;&&&NBSP;FIRSTROW0) { matrix[i][j] = 0; } else if (i > 0 && j > 0 && (matrix[0][j] == 0 | | matrix[i][0] == 0)) { matrix[i][j] = 0; } } } } ///////////// // solution b: extrarowandcol // private void setzeroes_extrarowandcol (Int[][] matrix) { if (matrix == null) return; // Assume it is m * n; int m = matrix.length; int n = matrix[0].length; boolean[] cols = new Boolean[m]; boolean[] rows = new boolean[n] ; for (int i = 0 ; i < m ; i ++) { for (int j = 0 ; j < n ; j ++) &nBsp { if ( matrix[i][j] == 0) { cols[i] = true; rows[j] = true; } } } for (int i = 0 ; i < m ; i ++) { for (int j = 0 ; j < n ; j ++) { if (Cols[i] | | &NBSP;ROWS[J]) matrix[i][j] = 0; } } }}
[Leetcode]73 Set Matrix Zeroes