首页 > 代码库 > clear one's rows and columns to 0 whose vlaue is 0

clear one's rows and columns to 0 whose vlaue is 0

 1 public class Q1_7{ 2  3 public static void SetZero(int[][] matrix){ 4  5 boolean [] row= new boolean[matrix. length];   6  7 boolean[] column =new boolean[ matrix[0]. length]; //列数 8  9 for(int i =0;i <matrix .length ;i ++){10 11 for(int j =0;j <matrix [0].length ;j ++){12 13 if(matrix [i ][j ]==0){14 row[i ]=true;15 column[j ]=true;16 }17 18 19 }20 21 22 23 }24 25 26 for(int i =0;i <matrix .length ;i ++){27 28 for(int j =0;j <matrix [0].length ;j ++){29 if(row [i ]||column [j ]){30 31 matrix[i ][j ]=0;32 33 }34 35 36 }37 38 }39 40 41 }42 43 44 public static void main(String[] args){45 46         int[][] matrix = new int[][]{47                         {1,2,3,4,},48                         {5,6,7,8,},49                         {9,1,2,4},50                         {2,0,4,9}51                      };52 53 54 55 SetZero( matrix);56 for(int i =0;i <4;i ++){57 for(int j =0;j <4;j ++){58 59 System.out.print( matrix[ i][ j]);60 61 62 }63 64 System.out.println();65 66 }67 68 }69 70 71 }

 

clear one's rows and columns to 0 whose vlaue is 0