publicclassProblem_73_MatrixToZero{publicvoidsetZeros(int[][] matrix){int m = matrix.length;int n = matrix[0].length;boolean flagRow =false;boolean flagCol =false;//先记录第一行本身是否包含0for(int i =0; i < n; i++){if(matrix[0][i]==0) flagRow =true;}//先记录第一列本身是否包含0for(int i =0; i < m; i++){if(matrix[i][0]==0) flagCol =true;}//用第一行和第一列记录for(int row =0; row < m; row++){for(int col =0; col < n; col++){if(matrix[row][col]==0){matrix[row][0]=0;matrix[0][col]=0;}}}//不能从0开始,上面记录的0会覆盖之前的值。for(int row =1; row < m; row++){for(int col =1; col < n; col++){if(matrix[row][0]==0|| matrix[0][col]==0){matrix[row][col]=0;}}}if(flagRow){for(int col =0; col < n; col++){matrix[0][col]=0;}}if(flagCol){for(int row =0; row < m; row++){matrix[row][0]=0;}}}}