Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

73. Set Matrix Zeroes #126

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

public class Solution { public void setZeroes(int[][] matrix) { if(matrix == null) return; Set rowset = new HashSet(); Set colset = new HashSet(); //行宽 int rowlen = matrix.length; //列长 int collen = matrix[0].length; for(int i = 0; i < rowlen; i++) { for(int j = 0; j < collen; j++) { if(matrix[i][j] == 0) { rowset.add(i); colset.add(j); } } } for(int row : rowset) { for(int i = 0; i < collen; i++) { matrix[row][i] = 0; } } for(int col : colset) { for(int i = 0; i < rowlen; i++) { matrix[i][col] = 0; } } } }

Shawngbk commented 7 years ago

image