解題報(bào)告: 因?yàn)椴灰胑xtra space 所以運(yùn)行時間可能有點(diǎn)高悦穿,主要是兩個方法浮声, 一個是找出這些零的位置怨咪,之后用第二個方法將它的縱和橫都改成零拭荤。
public class Solution {? ? public void setZeroes(int[][] matrix) {? ? ? ? if(matrix == null || matrix.length == 0|| matrix[0].length == 0) return;? ? ? ? int m = matrix.length;? ? ? ? int n = matrix[0].length;? ? ? ? //find which row and which column? ? ? ? HashMap> map = new HashMap<>();? ? ? ? ? ? ? ? for(int i = 0; i < m; i++){? ? ? ? ? ? for(int j = 0; j < n; j++){? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(matrix[i][j] == 0){? ? ? ? ? ? ? ? ? if(!map.containsKey(i)){? ? ? ? ? ? ? ? ? ? ? ArrayListtemp = new ArrayList<>();
temp.add(j);
map.put(i, temp);
}else{
map.get(i).add(j);
}
}
}
}
for(int i = 0; i < m;i++){
if(map.containsKey(i)){
for(Integer j: map.get(i)){
set(matrix, i, j);
}
}
}
return;
}
private void set(int[][] matrix, int i, int j){
//? if(i <= matrix.length || j <= matrix[0].length) return;
if(matrix == null || matrix.length == 0|| matrix[0].length == 0) return;
for(int k = 0; k < matrix[0].length; k++){
matrix[i][k] = 0;
}
for(int l = 0; l < matrix.length; l++){
matrix[l][j] = 0;
}
return;
}
}