I am trying to solve a problem using hashmap.
What I am trying to di is basically store the row no and column no as key and value respectively and when i==row no turn the entire row to 0.
also, when j==column no turn the entire column to 0.
<pre>import java.util.*;
public class Main {
public static void main(String[] args) {
int[][] matrix={{0,1,2,0},{3,4,5,2},{1,3,1,5}};
setZeroes(matrix);
}
public static void setZeroes(int[][] matrix) {
Map<Integer, Integer> index = new HashMap<>();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
index.put(i, j);
}
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (index.containsKey(i)) {
matrix[i][j] = 0;
}
if (index.containsValue(j)) {
matrix[i][j] = 0;
}
}
}
System.out.println(Arrays.deepToString(matrix));
}
}
The problem is in this part
if (index.containsKey(i)) {
matrix[i][j] = 0;
}
if (index.containsValue(j)) {
matrix[i][j] = 0;
}
Could someone please correct my logic?
What I have tried:
Solve the problem on paper and pen and then use the debugger