题解 | #清除行列#
清除行列
http://www.nowcoder.com/practice/c95aac1506574dfc8ad44c3939c6739d
清除行列,最主要的还是识别行和列,存入列表
public int[][] clearZero(int[][] mat, int n) {
HashSet<Integer> setRow = new HashSet<>();
HashSet<Integer> setCol = new HashSet<>();
// write code here
for (int i = 0; i < n; i ++) {
for (int j = 0; j < n; j ++) {
if (mat[i][j] == 0) {
setRow.add(i);
setCol.add(j);
}
}
}
int[][] result = new int[n][n];
for (int i = 0; i < n; i ++) {
for (int j = 0; j < n; j ++) {
if (setRow.contains(i)) {
result[i][j] = 0;
} else if (setCol.contains(j)) {
result[i][j] = 0;
} else {
result[i][j] = mat[i][j];
}
}
}
return result;
}
}