Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
/** *O(1)空间解法,把每行的第一个元素和每列的第一个元素作为改行列是否清零的标志 **/ public class Solution { public void setZeroes(int[][] matrix) { if (matrix==null) { return; } int row = matrix.length; int col = matrix[0].length; int[] zeroRow = new int[row]; int[] zeroCol = new int[col]; for (int i=0;i<row;i++) { for (int j=0;j<col;j++) { if (matrix[i][j]==0) { zeroRow[i] = 1; zeroCol[j] = 1; } } } for (int i=0;i<row;i++) { if (zeroRow[i]==1) { for (int pcol=0;pcol<col;pcol++) { matrix[i][pcol] = 0; } } } for (int i=0;i<col;i++) { if (zeroCol[i]==1) { for (int prow=0;prow<row;prow++) { matrix[prow][i] = 0; } } } } }