import java.util.Set;
public class Clearer {
public int[][] clearZero(int[][] mat, int n) {
// write code here
Set<Integer> row = new HashSet<Integer>();
Set<Integer> col = new HashSet<Integer>();
//找到所有的0所在位置
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(mat[i][j] == 0){
row.add(i);
col.add(j);
}
}
}
for(int i:row){
for(int j = 0; j<n;j++){
mat[i][j]=0;
}
}
for(int i:col){
for(int j = 0; j<n;j++){
mat[j][i]=0;
}
}
return mat;
}
}
题目描述
import java.util.Set;
public class Clearer {
public int[][] clearZero(int[][] mat, int n) {
// write code here
Set<Integer> row = new HashSet<Integer>();
Set<Integer> col = new HashSet<Integer>();
//找到所有的0所在位置
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(mat[i][j] == 0){
row.add(i);
col.add(j);
}
}
}
for(int i:row){
for(int j = 0; j<n;j++){
mat[i][j]=0;
}
}
for(int i:col){
for(int j = 0; j<n;j++){
mat[j][i]=0;
}
}
return mat;
}
}
请编写一个算法,若N阶方阵中某个元素为0,则将其所在的行与列清零。
给定一个N阶方阵int[][](C++中为vector<vector><int>>)mat和矩阵的阶数n,请返回完成操作后的int[][]方阵(C++中为vector<vector><int>>),保证n小于等于300,矩阵中的元素为int范围内。</int></vector></int></vector>
测试样例:
[[1,2,3],[0,1,2],[0,0,1]]
返回:[[0,0,3],[0,0,0],[0,0,0]]