class Solution {
public void setZeroes(int[][] matrix) {
int rows = matrix.length;
int columns = matrix[0].length;
Set<Integer> R = new HashSet<Integer>();//无序不可重复
Set<Integer> C = new HashSet<Integer>();
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
if(matrix[i][j] == 0){
R.add(i);
C.add(j);
}
}
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
if(R.contains(i) || C.contains(j)){
matrix[i][j] = 0;
}
}
}
//不需要返回值
}
}
归纳:
原地算法,即要求空间复杂度为O(1)
HashSet添加元素.add( ),判断是否存在.contains( )