Java Easy to Understand Solution (Accepted)

amazon
Tags: #<Tag:0x00007f242ca42a48>

#1
public void setZeroes(ArrayList<ArrayList<Integer>> a) {
    Set<Integer> row = new HashSet<>();
    Set<Integer> col = new HashSet<>();
    for(int i=0;i<a.size();i++){
        for (int j=0;j<a.get(i).size();j++){
            if(a.get(i).get(j)==0){
                row.add(i);
                col.add(j);
            }
        }
    }
    for(int i=0;i<a.size();i++) {
        for (int j = 0; j < a.get(i).size(); j++) {
            if(row.contains(i)||col.contains(j)){
                a.get(i).set(j, 0);
            }
        }
    }
}
indent preformatted text by 4 spaces

#2

I case where all the elements in array list are 0 the space complexity will reach to O(m)XO(n) and we need a solution in constant space. Can we optimise this?